我需要将数字以comma分隔的格式转换为以C#显示。

例如:

1000 to 1,000
45000 to 45,000
150000 to 1,50,000
21545000 to 2,15,45,000


如何在C#中实现呢?

我尝试了以下代码:

int number = 1000;
number.ToString("#,##0");


但是它不适用于lakhs

最佳答案

我想您可以通过创建满足您需求的自定义数字格式信息来做到这一点

NumberFormatInfo nfo = new NumberFormatInfo();
nfo.CurrencyGroupSeparator = ",";
// you are interested in this part of controlling the group sizes
nfo.CurrencyGroupSizes = new int[] { 3, 2 };
nfo.CurrencySymbol = "";

Console.WriteLine(15000000.ToString("c0", nfo)); // prints 1,50,00,000


如果只针对数字,那么您也可以

nfo.NumberGroupSeparator = ",";
nfo.NumberGroupSizes = new int[] { 3, 2 };

Console.WriteLine(15000000.ToString("N0", nfo));

关于c# - 将数字转换为逗号分隔的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16891030/

10-10 19:38