java - 双重精度损失

标签 java double

我有点困惑,因为我总是失去精度。

也许我使用了错误的类型。我有一个像“100.00”这样的字符串。但当我这样做时

Double.parseDouble("100.00") it is being cut off to 100. Need help. Thanks in advance.

最佳答案

您可能使用System.out.println(d)打印了您的号码。它将在内部调用 Double.toString(double),其规范说明

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double.

这是因为double数字没有“十进制精度”的概念。它是一个具有固定二进制精度的二进制数(二进制小数点后的二进制位数)。

关于java - 双重精度损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39914440/

相关文章:

跳过 Java 初始化字节码

c - 编写一个程序,读取一个整数。输出另一个整数,其中偶数位保留,奇数位减 1,

c - fscanf 没有正确读取 double

java - 将 POST 的有效负载记录到 Tomcat

Java抽象方法和接口(interface)

ios - 将字符串百分比转换为 double

C -- Double 类型数据 : Number slightly larger than allowed is able to slip through the conditions

swift - 如何在不快速四舍五入的情况下划分 double 值?

java - 未找到 Struts Tiles taglib 类

java - 使用 repaint() 进行奇怪的重新定位;