Den Random stuff
Den Random stuff

Reputation: 179

Remove 1.8E1 from double

When you have a big double: 1000000000D the result gets compressed into 1.0E8, how to make it print out 1000000000 and not 1.0E8.

Note: happens with 10000000000.10 too.

To solve, simply use BigDecimal or DecimalFormat, using DecimalFormat you can no longer edit the amount because it's now a string, but using BigDecimal is going to slow down you program.

Upvotes: 2

Views: 2914

Answers (3)

MaxZoom
MaxZoom

Reputation: 7763

You can use formatting flag as described in this page

double num = 1000000000D;
System.out.format("%10.2f", num);   // --> 1000000000.00

Upvotes: 3

Aleksandar
Aleksandar

Reputation: 1173

Example of using BigDecimal

    BigDecimal bd = new BigDecimal(1000000000d, new MathContext(0));
    System.out.println(bd);

or DecimalFormat

    DecimalFormat df = new DecimalFormat("#");
    System.out.println(df.format(10000000000000000000.0));

Upvotes: 3

lscoughlin
lscoughlin

Reputation: 2416

Depends on how you're printing it but the general answer is going to be, use the DecimalFormat.

That said, if you're using numbers of this magnitude, you may have a precision problem, and probably want to use BigDecimal.

Upvotes: 2

Related Questions