Question

I am trying to update few number values on a form. Once the values are shown without formation, I can easily change them to any number and correct number will be sent to the back-end; however, if I enter a big number such as 6000000, it will be changed to 6.0E7 on browser, but correct number will be sent to the back-end. I added fmt library to format the number but when I submit the form it sends 0 to the back-end.

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<s:textfield id="price" name="price" value="%{price}" theme="simple"/>  //6.0E7


<input name="price" id="price" value='<fmt:formatNumber value='${price}'/>'/> //0
Was it helpful?

Solution

You don't need to use JSTL's fmt, Struts2 has inbuilt Formatting Utilities:

Then instead of

<s:textfield name="price" value="%{price}" />  
<!-- output: 6.0E7 -->

use for example

<s:textfield name="price" value="%{getText('{0,number,#,##0.00}', {price})}" />
<!-- output: 6000000.00 -->

Also read about Type Conversion, and keep in mind that 6.0E7 is Scientific Notation (read more here).

OTHER TIPS

How using pattern property,

<input name="price" id="price" value=
    '<fmt:formatNumber value='${price}' pattern="##,####,###"/>'
/>

or type and groupingUsed like

<input name="price" id="price" value=
    '<fmt:formatNumber value='${price}' type="number" groupingUsed="false"/>'
/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top