簡體   English   中英

C#中1000,000格式的自定義數字組(千位分隔符)

[英]Custom number group (thousand separator) for 1000,000 format in c#

我的代碼如下所示,我正在嘗試對數字示例進行自定義分組:1000000應該是1000,000

我嘗試了很多搜索結果,允許使用千位分隔符和自定義分組,但每個結果都類似10,00,000

decimal value = decimal.Parse(txtamount.Text.Trim());
txtamount.Text = value.ToString("#,###0");

在這段代碼中,我還嘗試了以下代碼

txtamount.Text = String.Format("{0:n0}", value);
txtamount.Text = value.ToString("#,##0", new System.Globalization.CultureInfo("en-US"));
txtamount.Text = value.ToString("0,0.0",CultureInfo.InvariantCulture); //-->1,000,000.0

它不是數字的常規千位分隔符分組。 我正在嘗試以1000,000格式輸出

要僅使用一個分組分隔符,可以使用以下命令:

// create a writable copy of the culture of your choice
var ci = (CultureInfo)CultureInfo.GetCultureInfo("en-us").Clone();

// change the group sizes: the first one after 3 digits (counting backwards)
// NO second (third, ..) one!
ci.NumberFormat.NumberGroupSizes = new[]{3,0};

// and use it 
// the , in the format means "use a group separator", it does NOT specify a position
var millionString1 = 1_000_000.ToString("#,#", ci); // 1000,000
var millionString2 = 1_000_000.ToString("N0", ci); // also 1000,000

但請注意,現在一千萬將變成10,000,000。

參見docs

由於這是一種自定義格式,因此您需要轉義逗號,

string.Format("{0:####\\,###}",value)

如果沒有自定義格式器 ,這似乎是不可能的,但是可以在后面插入逗號。 例如 :

string value = "1234567.890"
string result = Regex.Replace(value, @"(\d+)(\d\d\d)", "$1,$2");  // result = "1234,567.890"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM