看代码。DecimalFormat df = new DecimalFormat("#.00");
double d = 63*0.105;
System.out.println(d);
System.out.println(df.format(d));
System.out.println(String.format("%.2f", d));
这边这个d只能用double型。。63*0.015应该为6.615 但是变为了6.61499999怎么破
double d = 63*0.105;
System.out.println(d);
System.out.println(df.format(d));
System.out.println(String.format("%.2f", d));
这边这个d只能用double型。。63*0.015应该为6.615 但是变为了6.61499999怎么破
public static double numberWithPrecision(double value, int precision) {
double temp = Math.pow(10, precision);
return ((int) Math.round(temp * value)) / temp;
} public static void main(String[] args) {
System.out.println(Hello.numberWithPrecision(6.615, 2));
}
}
double d=63*0.105;
double a=(d*1000+0.5);
int b=(int)a;
float c=b/1000f;
System.out.println(c);
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}