我有以下代码:
String price="9000.89";
double basePrice =10000;
String dis= String.valueOf(((basePrice - Double
.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);
它给我的输出为“9.991100000000007”,但我需要它的小数点后两位。知道如何做到这一点吗?
预期输出:9.99
最佳答案
String price="9000.89";
double basePrice =10000;
String dis = String.format("%.2f",((basePrice - Double.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);
关于java - 字符串精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34533900/