java - 字符串精度

标签 java string double decimal

我有以下代码:

 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/

相关文章:

java - 了解泛型以及如何避免转换

python - 如何分隔 'di' 单词中的前缀?

java - Oracle JDBC : underflow in double

java - Java 中的通用字段多态性

java - 为什么 eCobertura 和 EclEmma 测量的代码覆盖率不同?

Python float 到字符串 : how to get '0.03' but '-.03'

java.lang.NumberFormatException : For input string: "5

C# 操作溢出尝试将有效值分配给 double

java - 有没有更好的方法将 Double 转换为其精确的十进制字符串?

Java 1.5 过滤器 JTable