You need to sign in to do that
Don't have an account?
Alexis Kasperavicius
How to insert comma separators in TEXT numbers?
Hi all,
I am trying to make a field which displays this:
48 x 180 (8,640)
But I'm getting this (no separators):
48 x 180 (8640)
Here's the code I'm using:
TEXT(Lot__r.Unit_Count__c) & " x " & TEXT(Lot__r.kg_unit__c) &" (" & TEXT((Lot__r.Unit_Count__c * Lot__r.kg_unit__c)) & ")"
Is there a simple way to insert comma separators in TEXT numbers - or perhaps a better way to do this? I am stumped and think the only way must be to write some combination of RIGHT and TRIM statements - or something else? This must have come up before, right?
It's easy in APEX:
<apex:outputText value="{0, number, ###,###,###,###}"><apex:param value="{!Quote.Total_Price__c}"/></apex:outputText>
...but does not work in formula fields.
What am I missing? Thanks much for any help.
Alex
TO USE: In a text formula field, enter the below code and replace units__c with the number field that you need displayed with commas. Note: If this output will be needed in several places, consider creating a custom formula field with just this code (e.g. unitsText__c) and use that field where needed to keep things nice & tidy.
All Answers
This way i don't think you can achieve it, Even though if you are sure that you only want "," after the first digit from left then I can give you formula but if your multiplication record will have more ',' signis as multiplication result increases it wont be possible to maintaing by any formula. You may try with two different fields. One text another field can be number or currency whatever your multiplication result is. Use both on VFP to show your Text
48 x 180 (8,640)
Okay, well I don't think any application of mine would ever have more than 999 million as an answer, so what is the cleanest way you can think of to take an output of:
999999999
and make it
999,999,999
but also make sure that
999
doesn't become
,,999
?
Thanks much!
A
Here is sample code you can adapt to your formula:
if (Lot__r.Unit_Count__c * Lot__r.kg_unit__c < 1000, text(Lot__r.Unit_Count__c * Lot__r.kg_unit__c),
if(Lot__r.Unit_Count__c * Lot__r.kg_unit__c<1000000,text(floor( Lot__r.Unit_Count__c * Lot__r.kg_unit__c/1000))&','&text(right(Lot__r.Unit_Count__c * Lot__r.kg_unit__c, 3)),
if(Lot__r.Unit_Count__c * Lot__r.kg_unit__c<1000000000,text(floor( Lot__r.Unit_Count__c * Lot__r.kg_unit__c/1000000))&','&text(floor( Lot__r.Unit_Count__c * Lot__r.kg_unit__c/1000))&','&text(right(Lot__r.Unit_Count__c * Lot__r.kg_unit__c, 3)),"Error:Number Greater Than 999,999,999)
You should be able to see a pattern from there. It is not pretty, but it is the only way that I can see to accomplish what you want. I did not test it though so you may want to check the () and whatnot. Good Luck.
OK, nested IF statements. Interesting! Thanks very much for putting it together. There were a couple of minor tweaks and here is the tested code for anyone else who wants to do this:
I suppose the other way would be using LEN to figure this out.
I thought about len, and it would work, but you would have to have a case for each length beteen 4 and 9 inclusive so that would be 6 cases insead of 3.
Hi,
Are you sure, this works ??
I tried to have it in my dev org.
Used the code :
This worked fine for numbers upto 123,456 ..
But when I went over this number, I found that the code was missing something.
Here is the version that worked for me
I had to use the (MOD(NumberOfEmployees,1000000) in between to get it working fine.
Hope its useful
Shame on me for not testing all iterations. You're right. I marked yours as the solution. Thanks much!
Hi Lexlex,
is there any regex that we can use in our controller. I have number field. I want to show this number field with comma separated on email template.
One interesting thing to point out here...
I had a similar scenario with passing values to a Google Chart inside an IMAGE Function. Mine broke because there were decimal places in the number, which the client didn't want anyways. If the TEXT conversion results in a number with decimal places, this formula doesn't work. You'd have to use FLOOR inside ALL TEXT(fields) like this:
Only problem is, after all this, I was @ 5018 characters. Ugh... how to trim 18 charachters....
Hope that helps anyone down the road referencing this post...
If your formula gets too complex, you could always use a workflow field update.
IF(FIELD_NAME__c < 1000, TEXT(FLOOR(FIELD_NAME__c)),
IF(FIELD_NAME__c < 1000000, TEXT(FLOOR(FIELD_NAME__c/1000)) & ',' & RIGHT(TEXT(FLOOR(FIELD_NAME__c)),
3),
IF(FIELD_NAME__c < 10000000, TEXT(FLOOR(FIELD_NAME__c/1000000)) & ',' &
MID(text(FIELD_NAME__c), 4, 3)&","&
RIGHT(TEXT(FLOOR(FIELD_NAME__c)), 3),
IF(FIELD_NAME__c < 100000000, TEXT(FLOOR(FIELD_NAME__c/1000000)) & ',' &
MID(text(FIELD_NAME__c), 5, 3)&","&
RIGHT(TEXT(FLOOR(FIELD_NAME__c)), 3),
"Error:Number Greater Than 999,999,999") )
Any other possible solution to this issue?
What if any number is negative ,then it's working for me?
There are some other methods which can handle numbers above 1B using CASE, but they don't preserve decimals and push up the compiled size, this one keeps it under 2k. (Still, yikes!)
While you're here, be sure and vote for the idea (https://success.salesforce.com/ideaView?id=0873A0000003UnYQAU) to extend the TEXT function, so in future we can replace the monstrosity below with: TEXT(Number__c, "###,##0.00")
Please post here if you come up with a better way to do this. I still cringe every time I have to pull this out.
Again, please click here vote up this idea to make this standard functionality! (https://success.salesforce.com/ideaView?id=0873A0000003UnYQAU)
Here's a version of Alex's formula above that addresses negative values:
IF(Currency__c<0,IF(
Currency__c <= 1000000,
TEXT(FLOOR(Currency__c / 1000000)) & ",",
"") &
IF(
Currency__c <= 1000,
RIGHT(TEXT(FLOOR(Currency__c / 1000)), 3) & ",",
"") &
RIGHT(TEXT(FLOOR(Currency__c)), 3) & "." &
IF(
MOD(Currency__c , 1) * 100 < 10,
"0" & TEXT(ROUND(MOD(Currency__c , 1), 2) * 100),
TEXT(MIN(ROUND(MOD(Currency__c , 1), 2) * 100, 99))
),IF(
Currency__c >= 1000000,
TEXT(FLOOR(Currency__c / 1000000)) & ",",
"") &
IF(
Currency__c >= 1000,
RIGHT(TEXT(FLOOR(Currency__c / 1000)), 3) & ",",
"") &
RIGHT(TEXT(FLOOR(Currency__c)), 3) & "." &
IF(
MOD(Currency__c , 1) * 100 < 10,
"0" & TEXT(ROUND(MOD(Currency__c , 1), 2) * 100),
TEXT(MIN(ROUND(MOD(Currency__c , 1), 2) * 100, 99))
))
Thank you for sharing your code, it has been a real time saver. We have been using it to great effect.
Problem:
We recently had a number over 1,000,000 (rare) and noticed that the code wasn't producing the correct result. The number is 9,400,263.21 and the code was returning 9,002,263.21.
Possible Solution:
I combed through it and I think I have identified the culprit lurking in line 13. "1 +" should be removed from before "FLOOR". In the case of the example number, adding 1 to 9 takes the character length from 1 to 2 and causes the start point of the MID function to be knocked to the right by one character.
Question:
Is there a reason for the part of the code that I have just removed?
For more examples see the Developer guide here: apex:outputText (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_outputText.htm)
Any advice would be apprciated!
TO USE: In a text formula field, enter the below code and replace units__c with the number field that you need displayed with commas. Note: If this output will be needed in several places, consider creating a custom formula field with just this code (e.g. unitsText__c) and use that field where needed to keep things nice & tidy.
IF( AND(Unit__c< 1000,Unit__c> -1000) , TEXT( Unit__c),
IF( AND(Unit__c< 1000000,Unit__c>= 1000), TEXT(FLOOR( Unit__c/1000))
& ','
& RIGHT(TEXT( Unit__c), LEN(TEXT( Unit__c)) - LEN(TEXT(FLOOR( Unit__c/1000)))),
IF( AND(Unit__c< 1000000000,Unit__c>= 1000000), TEXT(FLOOR( Unit__c/1000000))
& ','
& MID(TEXT( Unit__c), 1 + LEN(TEXT(FLOOR( Unit__c/1000000))),3)
& ','
& RIGHT(TEXT( Unit__c), LEN(TEXT( Unit__c)) - LEN(TEXT(FLOOR( Unit__c/1000)))),
IF(AND(Unit__c> -1000000,Unit__c<= -1000), TEXT(FLOOR( Unit__c/1000))
& ','
& RIGHT(TEXT( Unit__c), LEN(TEXT( Unit__c)) - LEN(TEXT(FLOOR( Unit__c/1000)))),
IF(AND(Unit__c> -1000000000,Unit__c<= -1000000), TEXT(FLOOR( Unit__c/1000000))
& ','
& MID(TEXT( Unit__c), 1 + LEN(TEXT(FLOOR( Unit__c/1000000))),3)
& ','
& RIGHT(TEXT( Unit__c), LEN(TEXT( Unit__c)) - LEN(TEXT(FLOOR( Unit__c/1000)))),
IF( OR(Unit__c>= 1000000000,Unit__c<= -1000000000), "#TooBig!", NULL))))))
The problem I ran into with the proposed solutions is that the compiled character count quickly ran above the limits as I needed multiple values with separators in a single field.
I have rewritten the above (in my case 21,562 characters) to a simpler formula, which not only results in fewer characters (6,191) but is not limited to 3 sets of digits before the final "#TooHigh!" result.
In case you need more sets of digits, it's as simple as adding a line above with the location of the separator:
So, depending on the use case it could be perfect! Any chance you (or anyone else) wants to take a crack at handling decimals and negatives with this LEN method? I suppose ROUND could be used somehow, but pennies are often important, so maybe some kind of test for decimals? As or negatives, I have to think about it.
Thanks again for posting as I'm sure it will be used!
I am first invested in adding support for user locale, so I can be sure users see ',' or '.' in the correct use. For my usecase, decimals nor negatives are a thing I need to worry about, but I suppose if you know you always have double decimals (and you know the locale) you could try the following:
Otherwise a FIND function will allow you to calculate string length before decimals to update your LEN accordingly. Same for '-' you can FIND that and ignore that as a first character, or a simple IF on the left character
Can you please help to update this code up to 10000000000?
Hey guys, I have used above code to convert floats into strings several times now. Here is a consideration to include two decimal places. Cheers. - Christian
'$' +
if (LEN(TEXT($Record.Unit__c)) > 6, RIGHT(LEFT(TEXT($Record.Unit__c),LEN(TEXT($Record.Unit__c))-6),3) & ',',NULL)
& if (LEN(TEXT($Record.Unit__c)) > 3, RIGHT(LEFT(TEXT($Record.Unit__c),LEN(TEXT($Record.Unit__c))-3),3)
& ',',NULL) & RIGHT(TEXT($Record.Unit__c),3)
+
'.'
+
IF(LEFT(RIGHT ( TEXT ( {!$Record.Unit__c} ), 2 ), 1) = "0", "0", LEFT(RIGHT ( TEXT ( {!$Record.Unit__c} ), 2 ), 1)
)
+
IF( RIGHT ( TEXT ( {!$Record.Unit__c} ), 1 ) = "0", "0", RIGHT ( TEXT ( {!$Record.Unit__c} ), 1 )
)