In your profile, it says you are from South Korea. That's why I assume your current culture is ko-KR
. (And you said as well.)
And it's NumberDecimalSeparator
is .
but it's NumberGroupSeparator
is ,

Your Convert.ToDouble
works and it assumes your ,
is a thousands seperator, not decimal seperator. That's why your dthou
will be 1000
not 1
.
Convert.ToInt32(string)
uses Int32.Parse(string, CultureInfo.CurrentCulture)
explicitly and this method implemented like;
public static int Parse(String s, IFormatProvider provider)
{
return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
As you can see this method uses NumberStyles.Integer
as a default. And that's why your string can be successfully parsed only it contasion one of these;
- Leading white space
- Trailing white space
- Leading sign (positive or negative)
And since your string has thousands seperator or decimal seperator (this depends on which one you used for) this method throws exception.
Instead of that, you can use Int32.Parse(String, NumberStyles, IFormatProvider)
overload which you can specify your NumberStyles
like NumberStyles.AllowDecimalPoint
or NumberStyles.AllowThousands
As an example;
string str = "1,000";
int ithou = Int32.Parse(str, NumberStyles.AllowThousands,
new CultureInfo("ko-KR"));
Console.WriteLine(ithou); // Prints 1000
If you want to get 1
as a result, you can use CultureInfo.Clone
method to your culture and set it's NumberDecimalSeparator
and NumberGroupSeparator
properties like;
string str = "1,000";
CultureInfo c = (CultureInfo)CultureInfo.GetCultureInfo("ko-KR").Clone();
c.NumberFormat.NumberDecimalSeparator = ",";
c.NumberFormat.NumberGroupSeparator = ".";
int dthou = Int32.Parse(str, NumberStyles.AllowDecimalPoint, c);
Console.WriteLine(dthou ); // Prints 1
I don't think it's a cultural problem. It is the inconsistent handling
of the formatted string. ToDouble accepts strings with comma, but
ToInt32 does not. It's like going back to the original question again,
but couldn't ToInt32 be implemented to accept the comma just like
ToDouble function?
Oh my dear friend, you are still thinking wrong..
Everything is a culture problem in your case. There is no such a thing "Convert.ToDouble()
accepts strings with comma, but Convert.ToInt32()
does not".
Let's look at one more time how these methods are implemented.
Convert.ToDouble(string)
uses Double.Parse(value, CultureInfo.CurrentCulture)
explicitly and it is implemented like;
public static double Parse(String s, IFormatProvider provider)
{
return Parse(s, NumberStyles.Float| NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
}
With this NumberStyles.Float| NumberStyles.AllowThousands
, you can use both decimal point or thousands separator in your code but ,
is your culture's NumberGroupSeparator
not NumberDecimalSeparator
. That's why your string will be parsed as a thousands seperetor. There is no such a thing Convert.ToDouble uses string with comma. It can be use your current culture's NumberDecimalSeparator
or NumberGroupSeparator
depends on which character your string has. If both were equal, NumberDecimalSeparator
will be dominant and it will be used.
Convert.ToInt32(string)
uses Int32.Parse(string, CultureInfo.CurrentCulture)
explicitly and it's implemented like;
public static int Parse(String s, IFormatProvider provider)
{
return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
}
As I said before, NumberStyles.Integer
allows three things for your string; leading white space, trailing white space and leading positive or negative sign. It can't be parse if your string has decimal separator or thousands separator no matter it is comma or dot.
but couldn't ToInt32 be implemented to accept the comma just like
ToDouble function?
I told you before. Convert.ToInt32
doesn't have an overload takes NumberStyles
as a parameter. You can use Int32.Parse(String, NumberStyles, IFormatProvider)
overload which you can specify your NumberStyles
enumeration for parsing your decimal separator or thousands separator.