Toni Joe
Toni Joe

Reputation: 8417

decimal format for big numbers in java

While using the DecimalFormat object to format numbers in Java, I noticed that the largest number it can support is "1,000,000,000", so I tried to multiply it by 10 like this:

DecimalFormat df = new DecimalFormat("#,###,###,###.##########");

System.out.println(df.format(1000000000*10));

And this is what I got:

1,410,065,408

I didn't understand this result, shouldn't it be 1E10 ?

Does anyone of you guys knows how to fix this ?

Thanks

Upvotes: 1

Views: 501

Answers (1)

Codebender
Codebender

Reputation: 14448

The problem is not with the DecimalFormat, but with your integers.

When you do 1000000000*10, it goes out of range for int type and results in 1410065408.

You will need to perform the operation with long numbers instead.

System.out.println(df.format(1000000000l*10)); // Notice the l for long.

You will then see proper results.

Upvotes: 2

Related Questions