add commas using String.Format for number and

后端 未结 7 1070
执笔经年
执笔经年 2020-12-08 07:53

Using String.Format how can i ensure all numbers have commas after every 3 digits eg 23000 = \"23,000\" and that 0 returns \"0\".

String.Format(\"{0:n}\", 0); //give

相关标签:
7条回答
  • 2020-12-08 07:59

    you just put it like this:

    Console.WriteLine(**$**"Your current amount of money is: **{yourVar:c}**");
    
    0 讨论(0)
  • 2020-12-08 08:00

    You can do this, which I find a bit cleaner to read the intent of:

    String.Format("{0:#,###0}", 0);
    

    Example:

    string.Format("{0:#,###0}", 123456789); // 123,456,789
    string.Format("{0:#,###0}", 0); // 0
    
    0 讨论(0)
  • 2020-12-08 08:11

    You can put a number after the N to specify number of decimal digits:

    String.Format("{0:n0}", 0) // gives 0
    
    0 讨论(0)
  • 2020-12-08 08:17

    If your current culture seting uses commas as thousands separator, you can just format it as a number with zero decimals:

    String.Format("{0:N0}", 0)
    

    Or:

    0.ToString("N0")
    
    0 讨论(0)
  • 2020-12-08 08:19

    from msdn

    double value = 1234567890;
    Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
    

    Displays 1,234,567,890

    0 讨论(0)
  • 2020-12-08 08:20

    You can also play around a little with the CultureInfo object, if none of the other solutions work well for you:

            var x = CultureInfo.CurrentCulture;
            x.NumberFormat.NumberDecimalSeparator = ",";
            x.NumberFormat.NumberDecimalDigits = 0;
            x.NumberFormat.NumberGroupSizes = new int[] {3};
    
    0 讨论(0)
提交回复
热议问题