add commas using String.Format for number and

后端 未结 7 1071
执笔经年
执笔经年 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 08:20

    I up-marked another answer and then found that zero values are an empty string.

            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("de-DE");
            System.Threading.Thread.CurrentThread.CurrentCulture = ci;
    
            double x = 23232323.21;
            string y = x.ToString("#,0", System.Globalization.CultureInfo.CurrentCulture);
    

    The string is returned in the current culture which is German therefore y = 23.232.323

    y = 0 when x = 0.

    0 讨论(0)
提交回复
热议问题