[英]Custom number group (thousand separator) for 1000,000 format in c#
My code is as below I am trying to get custom grouping on number example: 1000000 should be 1000,000 我的代码如下所示,我正在尝试对数字示例进行自定义分组:1000000应该是1000,000
I have tried many search result that allows thousand separator and also custom grouping but each one gives a result like 10,00,000 我尝试了很多搜索结果,允许使用千位分隔符和自定义分组,但每个结果都类似10,00,000
decimal value = decimal.Parse(txtamount.Text.Trim());
txtamount.Text = value.ToString("#,###0");
In this code I have tried below code also 在这段代码中,我还尝试了以下代码
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
It is not a normal thousand separator grouping of a number. 它不是数字的常规千位分隔符分组。 I am trying to get output in a format 1000,000 我正在尝试以1000,000格式输出
To just have one grouping separator, you can use this: 要仅使用一个分组分隔符,可以使用以下命令:
// 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
But do note that 10 million would now become 10000,000. 但请注意,现在一千万将变成10,000,000。
由于这是一种自定义格式,因此您需要转义逗号,
string.Format("{0:####\\,###}",value)
Doesn't seem possible without a custom formatter , but the comma can be inserted after. 如果没有自定义格式器 ,这似乎是不可能的,但是可以在后面插入逗号。 For example : 例如 :
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.