I have created a function to help with this:
let currencyFormat = (number: real, symbol: string = "", separator: string = " ") {
//Convert number to string with 2 decimal places.
let StringNum = tostring(round(number, 2));
//Get the index of the .
let indexOfDot = indexof(StringNum, ".");
//Get the number without decimals
let Num = iif(indexOfDot > -1, substring(StringNum, 0, indexOfDot), StringNum);
//Get the decimal without number (includes .)
let Decimal = iif(indexOfDot > -1, substring(StringNum, indexOfDot), "");
//If decimal only has 1 character, add a 0 afterwards.
let DecimalFinal = iif(strlen(Decimal) == 2, strcat(Decimal, "0"), Decimal);
//Get the length of the Number part
let len = strlen(Num);
//Get the segments divided into 1000s
let seg1 = iif(len >= 3, substring(Num, len - 3, 3), Num);
let seg2 = iif(len >= 6, substring(Num, len - 6, 3), substring(Num, 0, len - 3));
let seg3 = iif(len >= 9, substring(Num, len - 9, 3), substring(Num, 0, len - 6));
let seg4 = iif(len >= 12, substring(Num, len - 12, 3), substring(Num, 0, len - 9));
let seg5 = iif(len >= 15, substring(Num, len - 15, 3), substring(Num, 0, len - 12));
//Output the final value
strcat(
symbol,
iif(strlen(seg5) > 0, strcat(seg5, separator), ""),
iif(strlen(seg4) > 0, strcat(seg4, separator), ""),
iif(strlen(seg3) > 0, strcat(seg3, separator), ""),
iif(strlen(seg2) > 0, strcat(seg2, separator), ""),
seg1,
DecimalFinal
)
};
Use it like so:
let currencyFormat = (number: real, symbol: string = "", separator: string = " ") {
let StringNum = tostring(round(number, 2));
let indexOfDot = indexof(StringNum, ".");
let Num = iif(indexOfDot > -1, substring(StringNum, 0, indexOfDot), StringNum);
let Decimal = iif(indexOfDot > -1, substring(StringNum, indexOfDot), "");
let DecimalFinal = iif(strlen(Decimal) == 2, strcat(Decimal, "0"), Decimal);
let len = strlen(Num);
let seg1 = iif(len >= 3, substring(Num, len - 3, 3), Num);
let seg2 = iif(len >= 6, substring(Num, len - 6, 3), substring(Num, 0, len - 3));
let seg3 = iif(len >= 9, substring(Num, len - 9, 3), substring(Num, 0, len - 6));
let seg4 = iif(len >= 12, substring(Num, len - 12, 3), substring(Num, 0, len - 9));
let seg5 = iif(len >= 15, substring(Num, len - 15, 3), substring(Num, 0, len - 12));
strcat(
symbol,
iif(strlen(seg5) > 0, strcat(seg5, separator), ""),
iif(strlen(seg4) > 0, strcat(seg4, separator), ""),
iif(strlen(seg3) > 0, strcat(seg3, separator), ""),
iif(strlen(seg2) > 0, strcat(seg2, separator), ""),
seg1,
DecimalFinal
)
};
MySource
| where Currency1 > 0
| project Currency1 = currencyFormat(Currency1, "$", ","),
Currency2 = currencyFormat(Currency2, "R", ","),
Currency3 = currencyFormat(Currency3)
INPUT:
Currency1 = 1254.3
Currency2 = 13231.02
Currency3 = 4213
OUTPUT:
Currency1 | Currency2 | Currency3
-----------|------------|-----------
$1,254.30 | R13,231.02 | 4 213.00