function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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().

 

GoCloudz SupportGoCloudz Support
It is 2023 and point mentioned by luckyme is still valid. There are scenarios when we would like to print the decimals on a PDF even if it is .00. We can always remove them if needed. Also, showing trailing zeros on PDF matches when users see on the UI.