Glenn Weinstein
I wanted to put a Formula field on the PSE Project detail page to show a red or green flag next to a currency amount, based on some criteria.
The flag image is the easy part. The hard part is displaying the currency amount with proper formatting (commas and decimal points). The TEXT() formula function results in a straight number, e.g. $1,532,311.78 turns into 1532311.78.
After much research, I determined there's no easy way to format numbers in conjunction with TEXT(). So I resorted to brute force, and wrote my own formula. After much trial-and-error, I'm pretty sure I've perfected it.
IF(someCurrencyField__c >= 1000000,TEXT(FLOOR(someCurrencyField__c / 1000000)) & ",","") &IF(someCurrencyField__c >= 1000,RIGHT(TEXT(FLOOR(someCurrencyField__c / 1000)), 3) & ",","") &RIGHT(TEXT(FLOOR(someCurrencyField__c)), 3) & "." &IF(MOD(someCurrencyField__c , 1) * 100 < 10,"0" & TEXT(ROUND(MOD(someCurrencyField__c , 1), 2) * 100),TEXT(MIN(ROUND(MOD(someCurrencyField__c , 1), 2) * 100, 99)))
Just replace someCurrencyField with your currency field, and you'll get back a nicely formatted number with 2 decimal points. Notes:
- Works for numbers up to 999,999,999.99. (If you need higher, just add another IF() clause.)
- Formula above assumes a multi-currency org. If not, replace CurrencyIsoCode with a hard "$" string.
- The last IF() clause is particularly tricky, to ensure we always get 2 digits to the right of the decimal point.
- That final MIN() is to guard against situations where someCurrencyField extends to more than 2 decimal points, to avoid inadvertently rounding up to 100 (we don't want, for example, $56,314.9999999 to come out as $56,314.100).
IF(something__c > somethingElse__c,IMAGE("/img/samples/flag_red.gif", "Red"),IMAGE("/img/samples/flag_green.gif", "Green")) & " " &TEXT(CurrencyIsoCode) & " " &/* copy in currency display code from above */
Then, pull the source field off your page layout, and drop in your new formula field. This technique adds some visual enhancement to otherwise-tedious looking displays of numbers:
I like putting the flags inline with the numbers - the flags help you quickly assess whether a number is "good" or "bad". And they look great as a column in a View list.
Note that you can run into a similar issue with Visualforce merge fields. And if you're using Visualforce for an email template, you can't even rely on solving formatting issues via the controller. But there is a much more straightforward solution to currency formatting in Visualforce, using the apex:outputText tag in conjunction with apex:param. Here's an example:
<apex:outputText value="{0, number, ###,###,###,##0}"><apex:param value="{!relatedTo.someCurrencyField__c}"/></apex:outputText>
Per the Visualforce Developer's Guide, you can use the same syntax as the Java MessageFormat class for your apex:outputText formatting string. Also, if you're showing currency (as I am here), you may want to precede your apex:outputText with a {!relatedTo.CurrencyIsoCode} merge field.







Glenn, I've been looking for a solution to display currencies in VF pages for PSE. Yours is a good start, but it's still missing a key requirement: that we display the correct thousands and decimal separators based on the user's locale. VF outputField handles everything perfectly... until you turn multi-currency on.
ReplyDeleteI don't see where $ or CurrencyISOCode is or should be in your original formula - does it just go before the first IF statement?
ReplyDeleteIt's good enough for my situation. Thanks.
ReplyDeleteoutputfield works great for me
ReplyDeleteI came across this issue when writing an Email template. It worked great in my DE Org but as soon as I tried to add it to my managed package it bombed with a binding error because the managed package DE is activated with multi-currency to support international HR.
ReplyDeleteI like the formula attempts but it is not going to work for many locale because for example in Europe it is common to separate the 1,000's with a period "." and the decimal as a comma "," e.g. 1,000,000.00 is expressed as 1.000.000,00 but the formula is better than doing nothing!
If you try to modify the formula for every possible combination and add the currency sign undoubtedly you hit the 5k character limit for formula and in a template you cannot us controllers.
I have raised it as a requested IDEAS fix. Please vote so we can get this sorted.
Formula works pretty well except for the points stated up there (multi currency).
ReplyDeleteThe main problem is that SF Doesn't work with locales at all even if SF states the Locale config at organization and user level.
There is only one thing missing in your formula, that is negative values... but the tweak is pretty easy, you can use an ABS function to get only positive part and add a "-" at the begginning if field__c < 0, easy.
I need to use this for the Spanish Euro format in VF pages, having to add the whole formula inside the value of the outputtext tag. Java formatting is not working as it uses "," and "." opposite to US locale.
when i was trying to display the currency in visualforce email template its showing me like 1.5E+7(exponential).
ReplyDeletecan anyone explain me how to overcome this pls..
thanks,
smk
smk - I'm not able to replicate your problem. Using my Visualforce example above (using the apex:outputText tag), amounts of any size seem to work fine (I tested with 9,999,999,999. Please post more specifics, such as the exact Visualforce code in your template. Or, contact me at glenn (at) appirio.com.
ReplyDeleteThis was super helpful. Thanks!
ReplyDelete@Anonymous - outputfield does not work for custom object fields, which is why this was helpful for me.