user3425746
user3425746

Reputation: 193

Maximum allowed digits after decimal for double

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.

Upvotes: 0

Views: 3325

Answers (2)

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

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.

Upvotes: 0

Dave
Dave

Reputation: 95

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

Use the BigDecimal class for fixed-scale arithmetic.

Upvotes: 0

Related Questions