2

I have a query that returns a decimal value. I'm looking for a way to format the value as currency. My current use case is limited to USD, but I'm sure others are looking for similar solutions.

I'd like to be able to go from "123456.789" to "$123,456.79".

4

2 回答 2

1

There isn't a currency data type in Azure Data Explorer. This generates the value you're describing but it ends up as a string which probably isn't what you want.

print Value=123456.789
| extend Currency = round(Value, 2)
| extend Decimal = round(Currency % 1, 2)
| extend WholeNumber = toint(Currency)
| extend Segment4 = WholeNumber / 1000000000 % 1000
| extend Segment3 = WholeNumber / 1000000 % 1000
| extend Segment2 = WholeNumber / 1000 % 1000
| extend Segment1 = WholeNumber % 1000
| project CurrencyString = 
    strcat(
        '$',
        iff(Segment4 > 0, strcat(tostring(Segment4), ','), ''),
        iff(Segment3 > 0, strcat(tostring(Segment3), ','), ''),
        iff(Segment2 > 0, strcat(tostring(Segment2), ','), ''),
        iff(Segment1 > 0, tostring(Segment1), ''),
        substring(Decimal, 1))

The team takes feature requests at https://feedback.azure.com/forums/915733-azure-data-explorer

[Updated to handle commas for numbers ... but still has plenty of flaws/limitations.]

于 2019-04-11T17:48:13.740 回答
1

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
于 2020-07-10T09:30:51.673 回答