Search code examples
javathymeleaf

Display normal (double) instead of scientific numbers - MVC Application


I know how to format a double like 9.78313E+10 that it display 97831300000 in Java.

But I have an MVC application and there is a getter at the view part for the double like:

<td>
    <input type="number" step="0.00000001" th:placeholder="'Current: '+  *{product.getTop()}" name="top"/>
</td>

My problem is that the view now displays 9.78313E+10.

A workaround would be a second getter for that object which formats that double to a String and returns it then.

Is there a more elegant way to solve that problem with Thymeleaf?


Solution

  • There is indeed a function provided by Thymeleaf to help you format numbers. Please check the official document of formatDecimal.

    String formatDecimal(Number target, Integer minIntegerDigits, Integer decimalDigits) 
    

    The first argument is the number you would like to format. The second argument is the minimum number of digits to display for the integer part, and the third argument is the maximum number of digits to display for the decimal part. So in your example, where you would like to format 9.78313E+10 into 97831300000, #numbers.formatDecimal(9.78313E+10, 11, 0) does the exact trick.