繁体   English   中英

如何在c#中格式化1700到1'700和1000000到1'000'000?

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

我喜欢格式化数学中的所有数字。 是否有预定义的函数或者是否可以使用子字符串和替换?

编辑:我的文化是de-ch

最好的祝福

试试这个

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

或这个

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

试试这个:

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

要么

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

使用int.ToString()iFormatProvider

另外看看这里msdn。

我总是使用这种格式

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

所以你可以使用它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM