在 Azure 数据资源管理器中将数字格式化为货币

Format a number as currency in Azure Data Explorer

我有一个 returns 十进制值的查询。我正在寻找一种将值格式化为货币的方法。我目前的用例仅限于美元,但我相信其他人也在寻找类似的解决方案。

我希望能够从“123456.789”变为“123,456.79 美元”。

Azure 数据资源管理器中没有货币数据类型。这会生成您所描述的值,但它最终会变成一个字符串,这可能不是您想要的。

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))

团队在 https://feedback.azure.com/forums/915733-azure-data-explorer

接受功能请求

[已更新以处理数字的逗号...但仍然有很多 flaws/limitations。]

我创建了一个函数来帮助解决这个问题:

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
    )
};

像这样使用它:

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)

输入:

Currency1 = 1254.3
Currency2 = 13231.02
Currency3 = 4213

输出:

 Currency1 | Currency2  | Currency3
-----------|------------|-----------
 ,254.30 | R13,231.02 | 4 213.00