how to format 1700 to 1'700 and 1000000 to 1'000'000 in c#?

≡放荡痞女 提交于 2019-12-22 07:48:29

问题


I like to format all numbers like in math. is there a predefined function or is that just possible with substring and replace?

edit: my culture is de-ch

Best regards


回答1:


Try this

int input = Convert.ToInt32("1700");
string result = String.Format("{0:##,##}", input);

Or this

Console.WriteLine(1700.ToString("##,##", new NumberFormatInfo() { NumberGroupSeparator = "'" })); 



回答2:


var numformat = new NumberFormatInfo {
                   NumberGroupSeparator = "'",
                   NumberGroupSizes = new int[] { 3 },
                   NumberDecimalSeparator = "."
                };
Console.WriteLine(1000000.ToString("N",numformat));



回答3:


Try this:

Console.WriteLine(1000000.ToString("#,##0").Replace(
    CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator, "'"));

Or

NumberFormatInfo likeInMath = new NumberFormatInfo()
{
    NumberGroupSeparator = "'"
};
Console.WriteLine(1000000.ToString("#,##0", likeInMath));



回答4:


use int.ToString() and an iFormatProvider.

also take a look here msdn.




回答5:


I always Use this format

 "#,##0;#,##0'-';0"

so You can use it in

 int input = Convert.ToInt32("100000000");  
 string result = String.Format("{#,##0;#,##0'-';0}", input);


来源:https://stackoverflow.com/questions/2100472/how-to-format-1700-to-1700-and-1000000-to-1000000-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!