.NET String.Format()在數千個數字中添加逗號

我想在數千個數字中添加一個逗號。 String.Format()嗎? git


#1樓

String.Format("{0:n}", 1234);  // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876

#2樓

String.Format("{0:#,###,###.##}", MyNumber)

這將在相關點給您逗號。 spa


#3樓

我之前再也不擔憂文化和潛在的格式問題的方法是,將其格式化爲貨幣,而後取出貨幣符號。 code

if (decimal.TryParse(tblCell, out result)) orm

{
  formattedValue = result.ToString("C").Substring(1);
}

#4樓

例如String.Format("{0:0,0}", 1); 返回01,對我來講無效 ip

這對我有用 ci

19950000.ToString("#,#", CultureInfo.InvariantCulture));

輸出19,950,000 string


#5樓

標準格式及其相關輸出, it

Console.WriteLine("Standard Numeric Format Specifiers");
String s = String.Format("(C) Currency: . . . . . . . . {0:C}\n" +
                    "(D) Decimal:. . . . . . . . . {0:D}\n" +
                    "(E) Scientific: . . . . . . . {1:E}\n" +
                    "(F) Fixed point:. . . . . . . {1:F}\n" +
                    "(G) General:. . . . . . . . . {0:G}\n" +
                    "    (default):. . . . . . . . {0} (default = 'G')\n" +
                    "(N) Number: . . . . . . . . . {0:N}\n" +
                    "(P) Percent:. . . . . . . . . {1:P}\n" +
                    "(R) Round-trip: . . . . . . . {1:R}\n" +
                    "(X) Hexadecimal:. . . . . . . {0:X}\n",
                    - 1234, -1234.565F);
Console.WriteLine(s);

輸出示例(使用咱們的文化): form

(C) Currency: . . . . . . . . ($1,234.00)
(D) Decimal:. . . . . . . . . -1234
(E) Scientific: . . . . . . . -1.234565E+003
(F) Fixed point:. . . . . . . -1234.57
(G) General:. . . . . . . . . -1234
    (default):. . . . . . . . -1234 (default = 'G')
(N) Number: . . . . . . . . . -1,234.00
(P) Percent:. . . . . . . . . -123,456.50 %
(R) Round-trip: . . . . . . . -1234.565
(X) Hexadecimal:. . . . . . . FFFFFB2E
相關文章
相關標籤/搜索