开发者

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

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

开发者_Go百科

edit: my culture is de-ch

Best regards


Try this

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

Or this

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


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


Try this:

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

Or

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


use int.ToString() and an iFormatProvider.

also take a look here msdn.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜