Decimal to string with thousand's separators?

后端 未结 3 1489
野趣味
野趣味 2020-12-20 21:13

Consider a Decimal value:

Decimal value = -1234567890.1234789012M;

i want to convert this Decimal value to a stri

3条回答
  •  被撕碎了的回忆
    2020-12-20 21:19

    The ToString method on decimals by default uses the CultureInfo.CurrentCulture for the user's session, and thus varies based on whom is running the code.

    The ToString method also accepts an IFormatProvider in various overloads. This is where you need to supply your culture-specific Formatters.

    For instance, if you pass the NumberFormat for fr-CH, you can format things as that culture expects:

    var culture = CultureInfo.CreateSpecificCulture("fr-CH");
    Decimal value = -1234567890.1234789012M;
    
    Console.WriteLine(value.ToString("##,#.###############", culture.NumberFormat));
    

    Will output

     -1'234'567'890.1234789012
    

    Edit #3 - rewrote using custom formatters. This should do what you want based on the new updated question.

    Edit #4 - Took all of your input, and ran this:

    public void TestOutput()
    {
        PrintValue(-1234567890M);
        PrintValue(-1234567890.1M);
        PrintValue(-1234567890.12M);
        PrintValue(-1234567890.123M);
        PrintValue(-1234567890.1234M);
        PrintValue(-1234567890.12347M);
        PrintValue(-1234567890.123478M);
        PrintValue(-1234567890.1234789M);
        PrintValue(-1234567890.12347890M);
        PrintValue(-1234567890.123478901M);
        PrintValue(-1234567890.1234789012M);
    }
    
    private static void PrintValue(decimal value)
    {
       var culture = CultureInfo.CreateSpecificCulture("qps-PLOC");
       Console.WriteLine(value.ToString("##,#.###############", culture.NumberFormat));
    }
    

    Gives output matching what you supplied:

    --12,,3456,,7890
    --12,,3456,,7890..1
    --12,,3456,,7890..12
    --12,,3456,,7890..123
    --12,,3456,,7890..1234
    --12,,3456,,7890..12347
    --12,,3456,,7890..123478
    --12,,3456,,7890..1234789
    --12,,3456,,7890..1234789
    --12,,3456,,7890..123478901
    --12,,3456,,7890..1234789012
    

    As pointed out by Joshua, this only works for some locales.

    From the looks of it then, you need to pick the lesser of two evils: Knowing the precision of your numbers, or specifying formats for each culture. I'd wager knowing the precision of your numbers may be easier. In which case, a previous version of my answer may be of use:

    To explicitly control the number of decimal places to output, you can clone the number format provided by the culture and modify the NumberDecimalDigits property.

    var culture = CultureInfo.CreateSpecificCulture("fr-CH");
    Decimal value = -1234567890.1234789012M;
    NumberFormatInfo format = (NumberFormatInfo)culture.NumberFormat.Clone();
    format.NumberDecimalDigits = 30;
    
    Console.WriteLine(value.ToString("n", format));
    

    This outputs:

    -1'234'567'890.123478901200000000000000000000
    

提交回复
热议问题