Ibrahim Aweidah
Ibrahim Aweidah

Reputation: 95

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. I could not do this with 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 " ". its the effect of the thousand seperator. How can I return it back to int. "536396".

Upvotes: 1

Views: 936

Answers (2)

Matthew Watson
Matthew Watson

Reputation: 109762

If you just want to convert to a string without the thousands rather than to an 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.

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 result = int.Parse(text, NumberStyles.AllowThousands, culture);

Upvotes: 5

Ravikumar
Ravikumar

Reputation: 625

Try this

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

Upvotes: 2

Related Questions