2

I want to display a percentage number in my visual force page and I want it to have 2 decimal places and a % sign following the number (eg. 2.50%).

I have a custom field of type Percent and I called it Rate__c in my custom object app__c.

I current have this line of code in my VF page please help me confirm if it is doing the right thing as I have asked?

<apex:outputText value="{0, number, ##.##}%"><apex:param value="{!app.Rate__c}"/></apex:outputText>

I have looked up and some other sources suggest it to be written like this:

<apexutputText value="{0, number, 000,000.00}">

I am also not quite understand what is the different between using the "#" vs using the 0 when formatting the number.

Thanks

4

In this case, it'd be easier to just use <apex:outputField>. It automatically applies formatting based on the field metadata (i.e. Type = percentage and decimals = 2).

This behavior is documented. From the documentation on <apex:outputField>(emphasis mine)

A read-only display of a label and value for a field on a Salesforce object. An <apex:outputField> component respects the attributes of the associated field, including how it should be displayed to the user. For example, if the specified <apex:outputField> component is a currency field, the appropriate currency symbol is displayed. Likewise, if the <apex:outputField> component is a lookup field or URL, the value of the field is displayed as a link.

|improve this answer|||||
  • when I just use the <apex:outputField> for some reason I have my number showed with like more then 6 or something decimal places. Mind you that the Rate__c is created with 2 decimal places when i set up the field so I don't know why it show up more than 2 decimal places when i rendered. So that is why I have to to a manual force to 2 decimal places – CB4 Nov 6 '17 at 19:58
  • @CB4 hmm, can't really look up any examples in my org at the moment, but I can say that it works correctly on percent formula fields. Might be worth double-checking your field definition. Beyond that, it might be worth it to put in a support ticket for it if you can reproduce the issue with a new percent field. About the only other suggestion I would have to use this solution would be to play around with Decimal.setScale() (since I'm fairly sure a percent is stored as a decimal). – Derek F Nov 7 '17 at 0:37
  • quick question though, is it wrong if I do it like this? {0, number, 00.00}%? – CB4 Nov 7 '17 at 15:12
  • @CB4 I'm not sure. You should play around with that yourself. Like my other answer says (I think you might've meant to comment on that one rather than this one), in Java, the % inside the format string multiplies the decimal being formatted by 100. I don't know if Apex does that as well, and the right string to use likely depends on how exactly percent fields are represented in memory. – Derek F Nov 7 '17 at 15:37
2

If you're hellbent on using <apex:outputText>, perhaps because you only want to display the field's value (and not its label as well), then you need to go beyond the Salesforce documentation.

The documentation on <apex:outputText> contains the following (emphasis mine)

Use with nested param tags to format the text values, where {n} corresponds to the n-th nested param tag. The value attribute supports the same syntax as the MessageFormat class in Java.

That should point you to some Java documentation, which tells you that a percent is treated as a NumberFormat. Going to the Java NumberFormat documentation, we see a subclass is DecimalFormat.

Java's documentation on DecimalFormat contains the answer to your question in the Patterns and Special Pattern Characters sections

Symbol    Location    Localized?    Meaning
0              Number     Yes                Digit
#              Number     Yes                Digit, zero shows as absent
.               Number     Yes                Decimal separator or monetary decimal separator
-               Number     Yes               Minus sign
,               Number     Yes                Grouping separator
E              Number     Yes               Separates mantissa and exponent in scientific notation.                                                        Need not be quoted in prefix or suffix.
;        Subpattern boundary Yes      Separates positive and negative subpatterns
%     Prefix or suffix   Yes                Multiply by 100 and show as percentage
\u2030    Prefix or suffix Yes        Multiply by 1000 and show as per mille value
¤ (\u00A4)    Prefix or suffix No        Currency sign, replaced by currency symbol. If doubled,                                                       replaced by international currency symbol. If present in a pattern,                                                          the monetary decimal separator is used instead of the decimal                                                            separator.
'      Prefix or suffix      No              Used to quote special characters in a prefix or suffix, for example,                                                        "'#'#" formats 123 to "#123". To create a single quote itself, use                                                        two in a row: "# o''clock".

So the difference between {0, number, 00.00%} and {0, number, ##.##%} is that "2.50%" would appear as "2.5%" in the second one. Note that the percent sign can go inside the NumberFormat string. That may or may not make a difference, depending on if your percentage is stored as "2.50" or "0.0250", you should work that out yourself.

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