apache commons中

round的源代码如下:

public static double round(double x, int scale, int roundingMethod) {

try {

return (new java.math.BigDecimal(Double.toString(x)).setScale(scale, roundingMethod)).doubleValue();

} catch (NumberFormatException ex) {

if (Double.isInfinite(x)) {

return x;

} else {

return Double.NaN;

}

}

}

我想知道,在创建BigDecimal时为什么他们选择将double转换为字符串(使用Double.toString)而不是简单地使用double本身?

换句话说,这有什么问题? :

public static double round(double x, int scale, int roundingMethod) {

try {

return (new java.math.BigDecimal(x).setScale(scale, roundingMethod)).doubleValue();

} catch (NumberFormatException ex) {

if (Double.isInfinite(x)) {

return x;

} else {

return Double.NaN;

}

}

}

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐