Reputation: 141
I have a JSP with some monetary amounts in £s. I want people viewing the same page from the US to see $ symbol instead of £s. (Forget for a moment that the values ought to be converted as well).
I am using this JSTL solution to set the locale
<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
<fmt:setLocale value="${language}" />
which works perfectly for output fields like this:
<fmt:formatNumber type="currency" value="${myOutputAmount}" />
but there are also some input fields that have the £ on its own before the input box, e.g.:
£<input type="text" id="myInputAmount"/>
How can I use that locale to show the relevant currency symbol instead of £?
I have searched and found solutions for iPhone, PHP, android, C# and Java. I could implement a Java solution in my JSP but the JSTL one was so neat I'm sure there must be an easy way, tapping into however that works.
Upvotes: 2
Views: 3010
Reputation: 1109132
There is unfortunately no JSTL tag for this.
In plain Java you could get it by Currency#getSymbol()
as follows:
String currencySymbol = Currency.getInstance(new Locale(language)).getSymbol();
// ...
You could wrap it around in a Javabean getter, an EL function or maybe a (shudder) scriptlet. You can find a concrete example of how to create an EL function near the bottom of this answer.
Update: you could use this "hack" to get the currency symbol out of a <fmt:formatNumber>
:
<fmt:formatNumber var="currencyExample" value="${0}" type="currency" maxFractionDigits="0" />
<c:set var="currencySymbol" value="${fn:replace(currencyExample, '0', '')}" />
...
${currencySymbol}<input type="text" name="amount" />
Upvotes: 1