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.