Maximum allowed digits after decimal for double

半世苍凉 提交于 2019-12-25 06:23:24

问题


Hi,

I want to know how many number of digits are allowed after decimal point for primitive double datatype in java, without actually getting rounded off.


回答1:


It depends on the number; read up on floating point representations for more information.

Use the BigDecimal class for fixed-scale arithmetic.




回答2:


Taken literally, the number is 0. Most decimal fractions with at least one digit after the decimal point get rounded on conversion to double. For example, the decimal fraction 0.1 is rounded to 0.1000000000000000055511151231257827021181583404541015625 on conversion to double.

The problem is that double is a binary floating point system, and most decimal fractions can no more be exactly represented in than 1/3 can be exactly represented as a decimal fraction with a finite number of significant digits.

As already recommended, if truly exact representation of decimal fractions is important, use BigDecimal.

The primitive double has, for normal numbers, 53 significant bits, about 15.9 decimal digits. If very, very close is good enough, you can use double.



来源:https://stackoverflow.com/questions/23274696/maximum-allowed-digits-after-decimal-for-double

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!