+ Start a Discussion
luckymeluckyme 

keep trailing zero with decimal format?

How come the format method on Decimal always strips trailing zeroes?

 

For example, if

Decimal d = 1234.00;

d.format() => 1,234

 

 

DevAngelDevAngel

I believe the format() function tries to produce a tidy representation of the double.  I mean, you wouldn't expect it to return 1234.0000, right?

 

It would be nice to have the Apex equivelent of DecimalFormat(d) though.

 

Cheers,

luckymeluckyme

I expect it to respect the scale of the decimal which is what toPlainString() does.

Decimal d = 1234567.00;

d.toPlainString() => '1234567.00'

d.format() =>  '1,234,567' now

but I think it should be '1,234,567.00'

If I want strip the trailing zeroes, I can always do

d.stripTrailingZeros().format().