1 根据规律写方法
numIncre(“000000”) => “000001”
numIncre(”0023") => “0024”
numIncre(“0009”) => “0010”
numIncre(“000099”) => “000100”
numIncre(“9”) => “0”
public static String numIncre(String num);
2根据规律写方法
round(123.455,2) => 123.46
round(123.449,2) => 123.45
round(123.44,3) => 123.440
round(123.4455,3) => 123.446
round(123.4499,3) => 123.450
round(123.9,0) => 124
public static double strRound(double value,int decimalPlaces);
[b]问题补充:[/b]
我要实现代码
import java.math.BigDecimal;
public class ts_main {
public static void main(String args[]) {
System.out.println(numIncre("00019"));
System.out.println(strRound(123.455,2)) ;
}
public static String numIncre(String str) {
return insertStr(getTotal(str), str.length()) + getTotal(str);
}
public static String insertStr(String _total, int num) {
String _str = "";
if (_total.length() < num) {
for (int i = 0; i < num - _total.length(); i++) {
_str += "0";
}
}
return _str;
}
public static String getTotal(String str) {
return Integer.parseInt(str) + 1 + "";
}
public static double strRound(double value,int decimalPlaces) {
if (decimalPlaces < 0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(value));
BigDecimal one = new BigDecimal("1");
return b.divide(one, decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
第一个,简单,加一,保留相同位数.
第二个,就是四舍五入,求精度问题.