4

Formatting numbers on a VF page, but I do a bunch of processing the Apex Class, so I would rather return the formatted String value from the controller.

I am doing something like this, but I just want the second number to appear as "(1500)" rather "-1500" Getting lost in the Message Format docs, so hoping someone can guide me in the right direction...

List<String> args = new String[]{'0','number','###,###,###'};
system.debug('Positive' + String.format(  ( (1000) - (500.50)  ).round().format(), args) );
system.debug('Negative' + String.format(  ( (-1000) - (500.50)  ).round().format(), args) );
  • Why can't you format using VF? I don't think it is possible to do what you want with String.format. The args you are passing aren't actually doing any formatting right now. Try changing the second line to system.debug('Args' + String.format('{0}, {1}, {2}', args) ); and I think you'll see what I mean. – Daniel Hoechst Sep 9 '13 at 23:06
12

Typically it's best to leave the formatting to the visualforce page and leave the controller to handle the model/controller side of things. You can easily do this in the Visualforce page and if you're using it in a lot of places just put it in a component you can reference all over the place.

<apex:outputText value="{0, number, ###,###,###}" rendered="{!value >= 0}">
    <apex:param value="{!value}"/>
</apex:outputText>
<apex:outputText value="({0, number, ###,###,###})" rendered="{!value < 0}">
    <apex:param value="{!value * -1}"/>
</apex:outputText>
| improve this answer | |
  • very nice reply – Pramod Kumar Sep 10 '13 at 12:55
  • Thanks - worked great. The component idea was especially helpful - I think I have been too resistant to their capabilities... – BritishBoyinDC Sep 10 '13 at 21:05
3

While Ralph Callaway's example is technically correct, it can be simplified.

There is no need for conditional rendering of the <apex:outputText> elements, or multiplying the value by -1. Instead we can use a subpattern for the negative number format.

From Java docs:

A DecimalFormat pattern contains a positive and negative subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a prefix, numeric part, and suffix. The negative subpattern is optional; if absent, then the positive subpattern prefixed with the localized minus sign ('-' in most locales) is used as the negative subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00". If there is an explicit negative subpattern, it serves only to specify the negative prefix and suffix; the number of digits, minimal digits, and other characteristics are all the same as the positive pattern. That means that "#,##0.0#;(#)" produces precisely the same behavior as "#,##0.0#;(#,##0.0#)".

By using ; to separate both the positive and negative subpatterns we can use a single <apex:outputText> to render both positive and negative values.

<apex:outputText value="{0, number, ###,###,###;(###,###,###)">
    <apex:param value="{!value}"/>
</apex:outputText>

When the value is positive, the subpattern ###,###,### is used and when the value is negative the subpattern (###,###,###) is used.

| improve this answer | |
1

Try creating a method in the controller to return the formatted string of a decimal:

public string negToParentheses (decimal d){
    if(d<0)
        return '('+string.valueof(-d)+')';
    return string.valueof(d);
}
| improve this answer | |

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.