gpt4 book ai didi

java - 字符串精度加倍

转载 作者:行者123 更新时间:2023-12-01 07:06:04 25 4
gpt4 key购买 nike

我确实有一个从文件中读取的字符串,其值为: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com