FormatNumber: how to add 2 decimal digits at the end of numbers?

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

I would like to show 10 as 10.00 or 0 as 0.00 with 2 decimal places.

how to do it?

+3
source share
4 answers

I believe you were looking for how to do this with the formatNumber tag

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

leads to

10.00
0.00

formatNumber uses DecimalFormat for parameterformat

+9
source

Java 5?

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

Java 4?

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

(from memory, may contain typos)

+5
source

NumberFormat:

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));

Junit.

DecimalFormat , String .

+3

You should use it as described in grails document

   <g:formatNumber number="${value}" minFractionDigits="2"/>
0
source

Source: https://habr.com/ru/post/1759271/


All Articles