簡體   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