转换数量逗号分隔值

问题描述 投票:2回答:6

我需要号码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

c# string.format
6个回答
6
投票

我想你可以通过创建自己需要的自定义数字格式信息做到这一点

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

2
投票

这里有一个类似线程你add commas in thousands place for a number

这里是与我的工作完美解决方案

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

     String.Format("{0:n0}", 9876); // no decimals

2
投票

如果你想成为独一无二的做,你不必在这里额外的工作是我的整数创建你可以在任何你想要的间隔放置逗号的功能,只是把3为每个千分之一逗号或者你可以做交替2或6或任何你喜欢的。

             public static string CommaInt(int Number,int Comma)
    {
     string IntegerNumber = Number.ToString();
     string output="";
     int q = IntegerNumber.Length % Comma;
     int x = q==0?Comma:q;
     int i = -1;
     foreach (char y in IntegerNumber)
     {
             i++;
             if (i == x) output += "," + y;
             else if (i > Comma && (i-x) % Comma == 0) output += "," + y;
             else output += y;

     }
     return output;
    }

1
投票

你有没有尝试过:

ToString("#,##0.00")

0
投票

快速而肮脏的方式:

Int32 number = 123456789;
String temp = String.Format(new CultureInfo("en-IN"), "{0:C0}", number);
//The above line will give Rs. 12,34,56,789. Remove the currency symbol
String indianFormatNumber = temp.Substring(3);

0
投票

一个简单的解决方案将是一个格式传递到ToString()方法:

string format = "$#,##0.00;-$#,##0.00;Zero";
   decimal positiveMoney = 24508975.94m;
   decimal negativeMoney = -34.78m;
   decimal zeroMoney = 0m;
   positiveMoney.ToString(format);  //will return $24,508,975.94
   negativeMoney.ToString(format);  //will return -$34.78 
   zeroMoney.ToString(format);      //will return Zero

希望这可以帮助,

© www.soinside.com 2019 - 2024. All rights reserved.