1

Possible Duplicate:
Convert.ToInt32() a string with Commas

i have a value in the label as: 12,000

and i wish to convert it into an integer like 12000 (use it for comparison)

i tried int k = convert.toint32("12,000"); this does not work.

Thanks

Community
  • 1
  • 1
user175084
  • 4,550
  • 28
  • 114
  • 169

4 Answers4

1

You're being screwed up by the comma. If all of your values have commas in them, you'll want to run a string.replace() to remove them. Once that comma is gone, it should work fine.

A more thorough way would be to Parse it, allowing for thousands.

guildsbounty
  • 3,326
  • 3
  • 22
  • 34
1

Try the following

var number = Int32.Parse("12,000", System.Globalization.NumberStyles.AllowThousands);
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

Try this

string num = "12,000"; int k = Convert.ToInt32(num.Replace(",",""));

sdmiller
  • 403
  • 2
  • 6
  • 18
0
 string k = "12,000";
 int i = Convert.ToInt32(k.Replace(",", ""));

will work

EvanGWatkins
  • 1,427
  • 6
  • 23
  • 52
  • 1
    This code is spectacularly culture-insensitive, and will fail when the application is run in a locale (and provided with a string) that uses `.` as the thousands' separator. – cdhowie Dec 08 '10 at 20:50