Virat
Virat

Reputation: 581

Double formatter to append trailing 0

I am using DecimalFormat class to format the double input to required format.

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

Then, I am using df.format(/*some value*/).

The problem is if I pass a double with more than 8 decimal part, it is formatting to 8 decimal part as required, but if I pass a value with less than 8 decimal part, it is not appending trailing 0's. How to achieve that?

Example

df.format(12.67543456667) will format it as 12.67543457

But,

df.format(12.675) will format it to 12.675 only.

How to format it to 12.67500000 ?

Upvotes: 1

Views: 760

Answers (3)

Nagma Firdose
Nagma Firdose

Reputation: 96

The same logic should work.

DecimalFormat df = new DecimalFormat("#.00000000");
//System.out.println(df.format(12.67543456667));`
System.out.println(df.format(12.675));

output:-

12.67500000

Upvotes: 0

Dariusz R.
Dariusz R.

Reputation: 96

DecimalFormat df = new DecimalFormat("#.00000000"); should works. Try to change symbols for the default FORMAT locale or pass DecimalFormatSymbols in constructor.

Upvotes: 1

achAmháin
achAmháin

Reputation: 4266

Yep, simply add:

df.setMinimumFractionDigits(8);

Upvotes: 1

Related Questions