Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL formatNumber for JSP custom pattern independent from language

Tags:

java

jsp

jstl

I'm developing a little app with JSP and I need to convert the European app to an international one (compatible with US format..etc). I've founded the pattern option for tag formatNumber here but it always depends on locale of your application.

Example 1:

I have a locale en_US and the formatNumber is:

 <fmt:formatNumber pattern="#,##0.00" value="${number}"/>

Result: 15,463,536,640.00

Example 2:

I have a locale es_ES and the formatNumber is:

 <fmt:formatNumber pattern="#,##0.00" value="${number}"/>

Result: 15.463.536.640,00

The thing it's that pattern it's related to locale! I need to use commas and dots independently from application locale because not always want to use locale format for show the numbers.

Any help?

like image 808
ferran87 Avatar asked Jun 03 '13 14:06

ferran87


1 Answers

Just explicitly set the locale.

<!-- Page's own locale (you should already have that part). -->
<fmt:setLocale value="${user.locale}" />
<fmt:setBundle ... />

... text ...

<!-- Temporarily set to English, format number and then set back to page locale. -->
<fmt:setLocale value="en_US" />
<fmt:formatNumber ... />
<fmt:setLocale value="${user.locale}" />

See also:

  • DecimalFormat is being overridden by server settings
like image 158
BalusC Avatar answered Oct 22 '22 22:10

BalusC