![](/img/trans.png)
[英]How can I convert String to Double without losing precision in Java?
[英]How can I convert a double to a string without notation and with a high amount of accuracy in Java?
我正在尝试将双精度转换为没有符号的字符串,并尝试了这个:
f= Double.valueOf(c.getString(c.getColumnIndex(NotesDbAdapter.KEY_VALUE)));
NumberFormat formatter = new DecimalFormat("###.##############");
但是,7 ^ 3 ^ 7的值返回为:558546000000000000反对558545864083284007。一如既往的帮助将不胜感激。
您已将值作为String。 为什么要将它转换为双倍?
你无法从一个它无法容纳的双精度中获得精确度。 558545864083284007
有18位小数。 double
精度为53位二进制精度,大约为15.9十进制数。 谷歌“每个计算机科学家都应该了解浮点数”。
###.##############
不适合558545864083284007
格式化掩码。
如果您已经有字符串格式的巨大十进制数,请尝试使用BigDecimal
类,如下所示:
BigDecimal bigDecimalValue = new BigDecimal("1234567890123456789012345678901234567890.54321");
LOGGER.info("bigDecimalValue: {}", bigDecimalValue.toPlainString());
您应该恢复原始值而不会出现精度损失:
bigDecimalValue: 1234567890123456789012345678901234567890.54321
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.