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.
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