views:

95

answers:

3

Hi,

I am using grails formatNumber and I would like to display my numbers in decimal format.

I would like to display 10 as 10.00 or 0 as 0.00 with 2 decimal digits.

how to do that ?

+5  A: 

Java 5?

 String.format("%.2f", (double)value);

Java 4?

 new BigDecimal(value).scale(2, RoundingMode.ROUND_HALF_UP).toString();

(from memory, may contain typos)

Aaron Digulla
+3  A: 

Or using the NumberFormat way:

NumberFormat formatter = new DecimalFormat("0.00");
Assert.assertEquals("10.00", formatter.format(10));
Assert.assertEquals("0.00", formatter.format(0));
Assert.assertEquals("0.10", formatter.format(0.1));

Asserting with Junit.

Have a look at the documentation for DecimalFormat for how to create the formatting String for the constructor.

Noel M
+5  A: 

I believe that you were looking for how to do this with Grails' formatNumber tag

<g:formatNumber number="${10}" format="0.00"/>
<g:formatNumber number="${0}" format="0.00"/>

results in

10.00
0.00

The formatNumber tag uses DecimalFormat for the format parameter

Colin Harrington
you are right .. thanks :D format='###,##0.00'
nightingale2k1