Reputation: 1
So I have
double a= 199812280115
When I
System.out.println(a);
I get
1.9981228E13
This representation of double a
does not imply that the value stored has been altered right?
Upvotes: 0
Views: 88
Reputation: 46395
You could use
System.out.format("%12f", a);
To see the number in its full glory.
Upvotes: 0
Reputation: 111279
YES, the output you see does show that the value has been altered.
Note that you see only 8 digits after the decimal point. The double
data type has enough precision for 15-16 digits after the decimal point. The default formatting in Java outputs the least amount digits that are required to distinguish the number from its neighbors so you should see all of the digits.
Besides, the exponent is wrong. The expected output is 1.99812280115E11
.
Upvotes: 1
Reputation: 4041
The double value printed in the console has been formated, according to the default toString() vaue, but the value remains the same. If you wish to print the value "as is", you should use a NumberFormat
to format and print.
Upvotes: 5
Reputation: 883
A good way to check if your value is altered is by doing something like this:
double a= 199812280115;
double b= 199812280000;
double c = a - b;
System.out.println(c);
You'll see yourself if your double has lost precision.
Upvotes: 1