java - 字符串精度加倍

标签 java string double precision

我确实有一个从文件中读取的字符串,其值为:38.739793110376837

当我使用以下方法将此值转换为 double 值时:

double.parseDouble("38.739793110376837");

我得到结果:38.73979311037684

如何在 double 变量中获得原始值?

我不想使用 BigDecimal 数据类型。

最佳答案

How can i have the original value in a double variable?

你不能。与原始值最接近的double正好 38.739793110376837148578488267958164215087890625

它被转换为“38.73979311037684”,因为这是唯一标识该 double 值的最短值。来自 documentation :

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. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0.

如果您想准确保留原始值,则应使用BigDecimal

关于java - 字符串精度加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23812764/

相关文章:

java - Java中.indexOf方法的算法选择

double - 我应该如何在 OpenCL 中启用 cl_khr_fp64?

java - 将 double 四舍五入到小数点后两位,显示 "#.00"而不是 "#.0"

java - 如何正确使用 StyledEditorKit 来对齐文本

java - 设置用作 JTable RowFilter 时 JComboBox 中显示的选项数量

java - LockSupport parkUntil 带阻挡器

c - 带字符串数组的 strcpy

java - Kafka流聚合: How to ignore intermediate aggregation results for a Window

javascript - 在 JavaScript 中使用动态(可变)字符串作为正则表达式模式

c# - 为什么 double.TryParse ("0.0000", out doubleValue) 返回 false ?