Trying to use Convert.ToInt32 on string value fails
-
venerdì 20 aprile 2012 04:08I'm trying to convert a string value, "1,400", to an int using Convert.ToInt32 and am told that Input string was not in correct format. Is the comma causing the problem? If so, is the best way around this using Replace to remove the comma? Thanks in advance.
Stan Benner
Tutte le risposte
-
venerdì 20 aprile 2012 04:22
You may, or you could use Int32.TryParse():
int i = 0; if (Int32.TryParse("1,234",System.Globalization.NumberStyles.AllowThousands, System.Globalization.CultureInfo.InvariantCulture, out i)) { Console.WriteLine(i); }- Proposto come risposta Syam S venerdì 20 aprile 2012 09:51
- Contrassegnato come risposta Bob ShenMicrosoft Contingent Staff, Moderator lunedì 7 maggio 2012 02:51
-
venerdì 20 aprile 2012 04:36
I'm trying to convert a string value, "1,400", to an int using Convert.ToInt32 and am told that Input string was not in correct format. Is the comma causing the problem? If so, is the best way around this using Replace to remove the comma? Thanks in advance.
Stan Benner
Hi,
you can perform like below...
String toParse = "1,400"; int num = int.Parse(toParse, NumberStyles.AllowThousands);
Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R
- Proposto come risposta Syam S venerdì 20 aprile 2012 09:51
- Contrassegnato come risposta Bob ShenMicrosoft Contingent Staff, Moderator lunedì 7 maggio 2012 02:51
-
venerdì 20 aprile 2012 04:42
Hello,
use this...........
string myStr = "123";
int myParsedInt = Int32.Parse(myStr);
int myConvertedInt = Convert.ToInt32(myStr);you can also refer this link for it.........
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx
http://www.developerfusion.com/thread/24964/converting-a-string-to-integerc/
Regards,
Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights
- Contrassegnato come risposta Bob ShenMicrosoft Contingent Staff, Moderator lunedì 7 maggio 2012 02:52
-
venerdì 20 aprile 2012 07:12
If you are not working in an English Environment, then set your system culture to an English Environment.
In most languages the comma is a decimal separator, not so clever to use with an Int.
Or like you wrote replace the comma by an empty string.
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx
English cultures are
"en-UK"
"en-US"but don't use
"en-ZA" in that the comma is also a decimal seperator.
Success
Cor- Contrassegnato come risposta Bob ShenMicrosoft Contingent Staff, Moderator lunedì 7 maggio 2012 02:51
-
venerdì 20 aprile 2012 08:35
-
venerdì 20 aprile 2012 10:01Yup. That's why I choose to use the overload with CultureInfo.InvariantCulture, which is guaranteed to use "," as thousand seperator and "." as decimal seperator and not to try luck with the current culture settings.
-
venerdì 20 aprile 2012 10:45
Yup. That's why I choose to use the overload with CultureInfo.InvariantCulture, which is guaranteed to use "," as thousand seperator and "." as decimal seperator and not to try luck with the current culture settings.
By whom, be aware that South African English (a true version of the English Language) uses the comma as thousand separator.
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx
The invariant has more to do with the date representation where in the English language in the US is a complete different format used then in the rest of the world.
Success
Cor
- Modificato Cor LigthertMVP venerdì 20 aprile 2012 10:45
- Modificato Cor LigthertMVP venerdì 20 aprile 2012 10:46
- Modificato Cor LigthertMVP venerdì 20 aprile 2012 10:46
-
sabato 21 aprile 2012 02:00
Humm... I think the whole point of introducing invariant culture is to make anything related to formatting running with the same behavior in every machine regardless of current system culture settings. All settings in this culture is guaranteed to be the same across systems, certainly not limited to the use of date formatting.
This is an important aspect of .NET runtime to know of if your application is going to run on systems in multiple countries. Always guard internal transformation (by internal I mean it's non-UI and never meant to be displayed to end-users directly) between string and other datatypes with InvariantCulture.
-
sabato 21 aprile 2012 06:15
Id do what you suggested (using the replace method on the string)... e.g:
string string_to_convert = "1,400"; string_to_convert = string_to_convert.Replace(",", ""); //removes the comma's int value = Convert.ToInt32(string_to_convert);
This will work.Personal Website: http://www.xanather.com
-
venerdì 27 aprile 2012 02:36Your code don't handle thousand seperator, so isn't answering the question.

