How to format currency number with proper positioning and fraction after decimals in Velocity template file?

272 Views Asked by At

I am using velocity template for generating pdf file. In this I have to show currency amount in the format like $ 7,242.84 or $ 0.00 or $ 410.70. I am using NumberTool class and in .vm file I am using something like $number.format("###0.00",$amount).

This is working fine in case if amount is 7242.8423 but if amount is 0.00 then it prints $ 0 if amount is 410.70 then $ 410.7.

Please tell me how I can use a proper formatting here.

2

There are 2 best solutions below

0
On

Java already defines multiple number/currency formats based on locale. Please see the following to get the details.

https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

0
On

I am not sure about velocity template but you should look for something like below

System.out.format("%.2f", 0.00f); //To ensure that it has two decimals or
System.out.format("%f", 0.00f);