66

I have a string that sometimes has commas seperating the number like 1,500 and I need to convert this to an Int, currently it is throwing an exception, can someone tell me how to fix this so that sometimes I may input numbers with commas and other times with out commas and it will still convert.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • Wouldn't you want to convert this to two ints? Use String.Split(). – Hans Passant Dec 01 '09 at 06:22
  • 1
    Remove commas before conversion. – big-z Dec 01 '09 at 06:21
  • i have the same problem @big-z. i have removed commas: bool totalPriceConversionResult = decimal.TryParse(txtTotalPrice.Text.Replace(",",""), out totalPrice) and convert it to: TotalPrice = totalPriceConversionResult ? Convert.ToInt32(totalPrice) : 0. but it gives me a 0 vaue. http://stackoverflow.com/questions/37359906 – Paolo Duhaylungsod May 21 '16 at 07:40

7 Answers7

170

You could use int.Parse and add the NumberStyles.AllowThousands flag:

int num = int.Parse(toParse, NumberStyles.AllowThousands);

Or int.TryParse letting you know if the operation succeeded:

int num;
if (int.TryParse(toParse, NumberStyles.AllowThousands,
                 CultureInfo.InvariantCulture, out num))
{
    // parse successful, use 'num'
}
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 5
    You're kidding, .Net can do that?! Nice! – Will Dec 01 '09 at 06:29
  • 2
    Very nice, use NumberStyles.Number flag if you want to parse greater numbers like billions. – MikeL Sep 02 '12 at 08:43
  • Bit old but in case some one runs across this. Best to use a combo flag here like NumberStyles.Number, or NumberStyles.Float, as it combines in the AllowThousands, as well as AllowTrailingSign and AllowDecimalPoint, which will break Double.Parse and Double.TryParse if you dont have them – Wanabrutbeer Sep 02 '20 at 19:03
  • 1
    Why does `int.Parse("1,23,4", NumberStyles.AllowThousands);` parse successfully? – The Fluffy Robot Dec 23 '20 at 23:29
9

You can use Decimal.Parse() then cast the result to int. This works with the current culture as well, or you can specify a CultureInfo to use. No need to manually handle commas, decimal points, etc, that's all built in to .NET.

Sam
  • 6,167
  • 30
  • 39
4

What's with the brute force suggestions of removing commas?? What if the string is something like "1,1,1"? Is this a valid number? Removing commas would make it a valid number and make the conversion semantically incorrect.

There's a reason NumberStyles exist and the Int.Parse() method can parse with or without regard to the number style.

Daria
  • 49
  • 1
  • 2
    `int.Parse("1,1,1", NumberStyles.AllowThousands);` returns `111`, so .NET does indeed think it is a valid number. – mr_plum Jul 27 '15 at 19:49
3

If you don't have to worry about rules for various countries (eg. some use comma as decimal position instead of thousands separator), then just strip out the commas first.

e.g

string nastyNumber = "1,234";
int result = int.Parse(nastyNumber.Replace(",", ""));

(replace int with double if you need floating point)

Will
  • 1,561
  • 9
  • 6
2

You can do replace(';', '') before you convert it to int.

liya
  • 782
  • 5
  • 6
2

You can use the code

int num = Strings.Replace(str, ",", string.Empty);

This replaces all occurrences of "," with "" (empty string). So 1,000,000 turns 1000000

Simpa
  • 151
  • 6
0

You can replace the commas with String.Empty prior to calling Convert.ToInt32(), or you can look into using Int32.Parse() or Int32.TryParse() with NumberStyles.AllowThousands as one of the parameters.

Int32.Parse

Int32.TryParse

Noctis
  • 11,507
  • 3
  • 43
  • 82
Jon Sagara
  • 1,122
  • 2
  • 11
  • 16