简体   繁体   English

C# 从千位分隔符转换回 int

[英]C# Convert back from thousand separator to int

I have a code that makes an int to a thousend separator but when I try to save this value in the text box to the database I want to remove the space between.我有一个代码可以将 int 转换为千位分隔符,但是当我尝试将文本框中的这个值保存到数据库时,我想删除它们之间的空格。 I could not do this with replace(" ","").我不能用 replace(" ","") 来做到这一点。

YearTB.Text = (String.Format(culture, "{0:n0}", Int32.Parse(YearTB.Text)));

What is in the box is for example "536 396" but the space between is not an " ".例如,盒子里的是“536 396”,但中间的空格不是“”。 its the effect of the thousand seperator.它的千位分隔符的效果。 How can I return it back to int.我怎样才能把它还给 int. "536396". “536396”。

If you just want to convert to a string without the thousands rather than to an int:如果您只想转换为没有千位的字符串而不是 int:

string result = text.Replace(culture.NumberFormat.NumberGroupSeparator, "");

This assumes that culture is the CultureInfo with which the number was formatted using a thousand separator.这假定culture是使用千位分隔符格式化数字的CultureInfo

Alternatively if you want to convert to an int, you can use int.Parse() directly and specify that you want to allow the thousands separator (which I think is a better approach):或者,如果要转换为 int,可以直接使用int.Parse()并指定要允许使用千位分隔符(我认为这是更好的方法):

int result = int.Parse(text, NumberStyles.AllowThousands, culture);

Try this试试这个

string str = "536 396";
str = Regex.Replace(str, @"\s", "");

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

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