Every so often someone asks how to format numbers in a string the same way that they appear in a number or currency field. Various formulas have been created in the past (including some that I’ve posted in threads on this topic), but a recent thread prompted me to take another stab at the concept, and I came up with the most condensed formula that I know of for formatting whole numbers.
The formula below will currently handle numbers—positive or negative—up to 21 digits long (<= 999 quintillion), though with a single change it could support higher values if needed.
IF(Number < 0, "-") &
REGEX_REPLACE(
ABS(Number) & "",
REPT("(\\d{3})", (ROUND((LEN(ABS(Number) & "") + 1) / 3)) - 1) & "$",
LEFT(",$1,$2,$3,$4,$5,$6", 3 * ((ROUND((LEN(ABS(Number) & "") + 1) / 3)) - 1))
)
If you want this displayed as currency, change the first line of the above formula to this:
IF(Number < 0, "-") & "$" &
To change the separators to periods for some other regions, replace the commas in the string that’s passed to the LEFT()
function in the next-to-last line of the formula; e.g.:
LEFT(".$1.$2.$3.$4.$5.$6", ...
To support even larger numbers, extend that string using the same pattern. Each additional [separator]$[number]
combo will add support for three more digits.
LEFT(",$1,$2,$3,$4,$5,$6,$7,$8", ...
I may revisit this later on to support numbers with decimals, but it’s tough to know how far down the road that might be.