Math.min不喜欢以0开头的数字0700

问题描述

我在使用Math.min时遇到了更多问题,所以当我尝试这样做时

System.out.println(Math.min(0700,1400));

它返回448而不是最小值0700。我知道当0不存在时它可以工作,但是由于用户输入,我有点需要将其格式化。有什么办法解决这个问题,也许是一种替代方法或一种快速有效的方法来消除0,然后再将其放入Math.min参数。我可以做一个if语句,先检查然后解析substring,但这似乎很乏味且效率低下。有什么想法吗?

解决方法

在任何带有前导0的输入上调用以下函数(假定它们是字符串),然后使用Integer.parseInt将新字符串转换为整数,然后继续使用常规的Math.min函数最后一个整数:

//This function takes in a string that is of the form of a integer with 
//leading zeros i.e. it would take in 0008000 and return the string 8000
public static String removeZeros(String str) {
    String regexpattern = "^0+(?!$)"; //regex pattern to find leading zeros
    str = str.replaceAll(regexpattern,""); //remove leading zeros in string
    return str; //return string
}