6

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?

Dasrath
  • 366
  • 2
  • 11
ferran87
  • 635
  • 1
  • 6
  • 17
  • I don't understand what you want. You want to format the number according to the locale of the user of the application, or you want to format the number according to a fixed locale, identical for all the users of the application? In any case, you just need to set the appropriate locale. – JB Nizet Jun 03 '13 at 14:49
  • Yes, change the locale is the solution. But I don't want be changing the locale for each line. I want put directly in a pattern and that it respects the commas and dots. – ferran87 Jun 04 '13 at 07:26

3 Answers3

6

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you BalusC. I knew this solution, but I was looking for a more elegant solution. Probably I'll do my own tag... – ferran87 Jun 04 '13 at 07:23
0

Inside your own tag-file:

  • Set locale to en_US
  • Print the desired number with your pattern.
  • Revert to old locale.

The tag file like this:

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

<%@attribute name="number" description="The number to format and print." %>

<fmt:setLocale value="en_US"/>
<fmt:formatNumber pattern="#,##0.00" value="${number}"/>
<fmt:setLocale value="${user.locale}"/>

Alternatively, you could add the pattern as in input parameter, if you wish to not hardcode it.

Gunslinger
  • 729
  • 1
  • 9
  • 19
-1

What about DecimalFormat

DecimalFormat formatter = new DecimalFormat("###,###,###");
khr055
  • 28,690
  • 16
  • 36
  • 48
  • 2
    This answer has several problems: 1) `` already uses `DecimalFormat` under the covers. 2) This answer doesn't take concrete problem wrt the locale anywhere into account. 3) Writing Java code in JSP is bad practice. Taglibs like JSTL should be used. – BalusC Jun 03 '13 at 14:59
  • 1
    I don't mean to use Java in JSP, maybe I have to explain better, As far as I know he wants to format a number in a specific way, no matter locale. Why don't do it in the server? – Giancarlo Muñoz Jun 03 '13 at 15:04
  • 1
    The `DecimalFormat` **is** locale specific, but you're nowhere explicitly mentioning/explaining that. The result of your code depends on the system default locale. Please carefully read "See also" link in my answer. – BalusC Jun 03 '13 at 15:11