4

How do I format a number in an outputText element so that it uses spaces as a separator between thousands?

What I currently have is an outputText element which formats number using comma as a separator (e.g. 1,000,000.00).

 <apex:outputText value="{0, number, ###,##0.00}">
     <apex:param value="{!item.numberValue}"/>
 </apex:outputText>

However, that doesn't correspond to the user's country locale, where it is usual to have a space as a separator between thousands (1 000 000,00). How do I achieve that? I have tried to add space surrounded by single quotes (which should work according to java MessageFormat) and all sorts of escape characters, but the field ignores the space (resulting in 1000000.00) and takes escapes as literals (with the result looking like: 1000000.00\b).

Any help will be much appreciated.

3

This is a known behavior which some of us don't like. There seems to be no perfect solution but some workarounds.

The approach is to create the localized string on your own instead of letting salesforce do it.

http://www.interactiveties.com/b_locale_datetime.php#.VGzAacm-O2U Format decimal in <apex:outputText> respecting the user's Locale

You are not alone and may also vote for this idea: https://success.salesforce.com/ideaView?id=08730000000i6AaAAI

EDIT:

  1. Another approach is to use an apex:outputField instead of outputText. There the localization is working as expected and the locale including separators, time and date formats are supported.

  2. Then you could use JavaScript to swap the separators. Easiest way is to use jquery and wrap the outputText with some div to have easy selectors. Firebug is my recommendation to figure out the selectors.

All depends a bit on you skills and what you want to do with the formatted text.

  • Thanks. I don't really need the formating to be dynamic and based on the respective user's locale. That would be by far the best solution, however I have already made my peace with the fact that that is not possible without a workaround. What I need is a space use statically for every user as a thousand separator. Can't do it, though. – Tomáš Kolín Nov 19 '14 at 16:24
  • 1
    Added some more ideas for workarounds. Why not use inputField instead outputText? – Uwe Heim Nov 19 '14 at 16:32
  • Because what I am displaying is a result of a run-time calculation, not an SObject field. JQury/javascript would work, it would be ugly, though :-) Worst case scenario, I'd have to go for it, though. – Tomáš Kolín Nov 19 '14 at 16:35
  • another two ideas for that: check if you can use a Formula for you calculation. If your logic is too sophisticated for Formulas use a trigger to write the result to a helper field and then go with outputField. Awkward yes, but welcome the realm of workarounds... – Uwe Heim Nov 19 '14 at 16:40
  • Can use neither trigger nor formula, because I don't have any sObject in play. It's just pure calculated stuff in variables. – Tomáš Kolín Nov 19 '14 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.