studyAccount
studyAccount

Reputation: 1

Is my double being altered?

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

Answers (4)

Floris
Floris

Reputation: 46395

You could use

System.out.format("%12f", a);

To see the number in its full glory.

Upvotes: 0

Joni
Joni

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

Cristian Meneses
Cristian Meneses

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

ChristopherS
ChristopherS

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

Related Questions