Search code examples
parsingtypesjstl

Why does JSTL parseNumber parse 1000.0 to 10000?


I have an input form where I want to insert some numbers and calculate some results. So my input field looks like

<fmt:parseNumber var = "a" type = "number" 
    value = "${object.someAttribute}" integerOnly = "true"/>

<input type="number" name="someAttribute" required pattern="[0-9]" value="${a}" />

I want to do following: at first visit, a user should insert a number (Integer). In the calculation, all values are Double to prevent casting side effects. When the site is refreshed / the user wants to repeat the calculation, the input field should be preset with the recently used value. Therefore I tried fmt:parseNumber to parse the Double value from the object to an Integer.

At first try I omitted integerOnly = "true" but got an Error (as '1000.0' is not a valid input, that's understandable as I specified pattern="[0-9]").

But with integerOnly set, it changes the value from 1000.0 to 10000. What am I doing wrong? How could I parse it to achieve my goal?


Solution

  • If you are using EL 2.2+, you can simply convert a double to integer by invoking a non-getter method on your Double object:

    ${yourDouble.intValue()}
    

    See: