I am migrating from an old MFC GUI to C#.
I was building a Form-based GUI when I've got an unexpected exception converting a string to an integer type. I assumed it would work the same as converting string to double.
string str = "1,000";
double dthou = Convert.ToDouble(str); // OK
int ithou = Convert.ToInt32(str); // raises an exception
Conversion to double gives correct value: 1000.0.
For int conversion, I was able to get a solution : Convert.ToInt32() a string with Commas.
But I am curious if there were any reason behind this.
Or, am I missing something?
I was able to find a similar, but not exactly a duplicate question :
Number parsing weirdness
[EDIT] after learning about the culture issue.
I am in a kind of a culture-shock because until now, in Korea, both floating point number and integer numbers are expressed with "," for thousands group and "." for decimal point (at least in the real world, in Korea, I mean, I think... ).
I guess I will have to accept current settings of MS Visual Studio and carry on.
[EDIT2] after sleeping over this issue.
I think it's more of the inconsistent handling of the formatted string. ToDouble accepts strings with thousands separator (in my culture, comma), but ToInt32 does not. If ToDouble is float | allowThousands, then why could'nt ToInt32 have been integer | allowThousands is what I am asking.
For the double conversion, there are two possibilities:
In your culture, , is the number group separator. And so the conversion succeeds and returns a value of 1000.
Alternatively, in your culture, , is used as the decimal separator. Again the conversion to floating point succeeds but this time returns 1.
For conversion to integer, "1,000" is simply not an integer. My suspicion, given your naming, is that , is a number group separator for you. And you are expecting it to be treated that way by ToInt32(). But ToInt32() does not accept number group separators. Valid characters for ToInt32() are 0 to 9, the optional sign prefix of - or + and leading or trailing whitespace.
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.
In the english culture the decimal sign is "." . In the swedish culture the decimal sigh is ",". The myriad sign can be " ", "," or ".". So that's why C# throws an exception when the decimal sign is different than its culture specified.
Related
I am trying to convert 2.3449 to Decimal but It converts like 23449,0
I am converting it like below
string temp = "2.3449";
decimal value_ = Convert.ToDecimal(temp);
if I replace the DOT with COLON, it converts it perfectly. But, I dont want to Replace the DOT with COLON in string. What is the good way of converting without replacing.
Your culture treats . as the thousands separator, rather than decimal separator.
You can always use a specific culture:
var val = decimal.Parse(temp, CultureInfo.InvariantCulture);
The same goes for ToString - if you want to print the number with . as the decimal separator, just use the appropriate culture. The local culture (the default) is usually the best bet for anything user-facing, though. Invariant culture is perfect for persistence :)
In C#:
This throws a FormatException, which seems like it shouldn't:
Int32.Parse("1,234");
This does not, which seems normal:
Single.Parse("1,234");
And surprisingly, this parses just fine:
Single.Parse("1,2,3,4"); //Returns 1234
My local culture is EN-US, so , is the default thousands separator char.
Main question: Why the inconsistency?
Also: Why does Parse("1,2,3,4") work? It appears to just be removing all instances of the local separator char before parsing. I know there would be extra runtime overhead in a regex check or something like that, but when would the numeric literal "1,2,3,4" not be a typo?
Related:
C# Decimal.Parse issue with commas
According to MSDN:
The s parameter contains a number of the form:
[ws][sign]digits[ws]
The s parameter is interpreted using the NumberStyles.Integer style. In addition to decimal digits, only leading and trailing spaces together with a leading sign are allowed.
That's it, NumberStyles.Integer disallows the Parse method to use the thousands separator, whereas Single.Parse uses by default NumberStyles.Float and NumberStyles.AllowThousands. You can change this behaviour by specifiying the second argument as NumberStyles:
Int32.Parse("1,234", NumberStyles.AllowThousands); //works
Single.Parse ignores the grouping and doesn't use culture-specific NumberGroupSizes at all, and only determines if the character is a group or decimal separator. The group sizes are used only when formatting numbers.
For the first case, from Microsoft Source Code Reference, by default Int32.Parse implements NumberStyles.Integer but not NumberStyles.AllowThousands
public static int Parse(String s) {
return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
Thus any comma separator is not allowed. This:
Int32.Parse("1,234");
or
Int32.Parse("1.234");
will both be wrong. In any culture.
To fix it, NumberStyles.AllowThousands must be added to the NumberStyles which will allow "1,234" to be parsed in EN-US culture:
Int32.Parse("1,234", NumberStyles.Integer | NumberStyles.AllowThousands);
But
Int32.Parse("1.234", NumberStyles.Integer | NumberStyles.AllowThousands);
Will still throw an Exception.
For the second case, according to Microsoft Code Source Reference, the default style for Single.Parse is:
public static float Parse(String s) {
return Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
}
Which allows thousands separator. And "," is recognized as thousand separator in EN-US culture, for Single.Parse and thus you get the second case parsed correctly
Single.Parse("1,234"); //OK
And obviously "1.234" will also be correct, except that "." is not recognized as thousand separators but decimal separator.
As for the third case, Internally, Single.Parse calls TryStringToNumber, and Parse.Number which would simply ignore the thousand separators. Thus you get:
Single.Parse("1,2,3,4"); //Returns 1234
Because it is equivalent as
Single.Parse("1234"); //Returns 1234
mine is es-ES
On these . is the default thousands separator char, and "," the separate character between int and double
So
any parse like "1.2.3,4" gives me "123,40" ( 123.40 on US )
If i put the "." before the "," like "123,4.3" it gives error
but, the same way the questions says, if i put "1.2.3.4" gives me "1234"
So, may be it is a functionality of the .net itself.
I want a price to be a decimal and for the decimal-sign to be localised. Depending on culture, a decimal may use , or ..
For now I am doing this:
subItem.price.ToString("#.#", CultureInfo.InvariantCulture);
But if the price is 0 (with no decimal point), the result of that turns out to be "" and that makes my system crash.
Is there a better pattern than "#.#" that might handle the 0 in a better way?
How about using "0" custom format specifier instead?
Console.WriteLine((0).ToString("0.0", CultureInfo.InvariantCulture));
prints
0.0
Your code returns empty string because from The "#" custom format specifier
Replaces the "#" symbol with the corresponding digit if one is
present; otherwise, no digit appears in the result string.
Note that this specifier never displays a zero that is not a
significant digit, even if zero is the only digit in the string. It
will display zero only if it is a significant digit in the number that
is being displayed.
By the way your title and your question says different things. Your question doesn't relevant with what number decimal separator is used when you format your value. It is all about why you get empty string when you format your 0 value.
Try 0.#. Will display 0 if value is 0.0
Possibly try using:
subItem.price.ToString("{0:C}", CultureInfo.InvariantCulture);
it formats your string as a currency.
This is probably an old one but I can't find anything on it. Does anyone have any idea why this:
Convert.ToDecimal("3.14521963414679E-08")
throws
FormatException ("Input string was not in a correct format.")
However this works as expected?
Convert.ToDouble("3.14521963414679E-08")
I thought Convert.ToDecimal could handle exponentials - maybe I got that wrong.
Convert.ToDecimal doesn't support the scientific notation.
It is documented that Convert.ToDecimal internally uses Decimal.Parse and the documentation for Decimal.Parse states that this uses NumberStyles.Number and thus only the following is a valid input:
[ws][sign][digits,]digits[.fractional-digits][ws]
To support scientific notation you will have to use another overload of Decimal.Parse that allows you to specify the NumberStyles to be used:
var result = decimal.Parse("3.14521963414679E-08",
NumberStyles.Number | NumberStyles.AllowExponent);
Convert.ToDecimal() method uses Decimal.Parse() explicitly.
From MSDN;
Indicates that the numeric string can be in exponential notation. The
AllowExponent flag allows the parsed string to contain an exponent
that begins with the "E" or "e" character and that is followed by an
optional positive or negative sign and an integer. In other words, it
successfully parses strings in the form nnnExx, nnnE+xx, and nnnE-xx.
It does not allow a decimal separator or sign in the significand or
mantissa; to allow these elements in the string to be parsed, use the
AllowDecimalPoint and AllowLeadingSign flags, or use a composite style
that includes these individual flags.
You can use Decimal.Parse Method (String, NumberStyles) overload of this method which allows you to use NumberStyles enumration like;
Decimal.Parse("3.14521963414679E-08",
NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);
Here a DEMO.
Because of this->
ToDecimal(String)--Converts the specified string representation of a number to an equivalent decimal number..
ToDouble(String)--Converts the specified string representation of a number to an equivalent double-precision floating-point number.
for further clarification visit-
http://msdn.microsoft.com/en-us/library/System.Convert_methods.aspx
Hope it helps..:)
I have a line like the following in my code:
string buffer = string.Format(CultureInfo.InvariantCulture, "{0:N4}", 1008.0);
Why does buffer contain 1,008.0 (note comma) after executing this line?
Yes, I do guess that it's caused by my regional settings. The question is why they affect the result in this case?
EDIT:
Ok, I understand that it's completely my fault. It seems like I should have used F format specifier.
The InvariantCulture is loosely based on en-US which uses , as a thousands (group) separator.
Your result is what I would expect.
I also point you to the details of the N numeric format specifier:
The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9), "," indicates a group separator, and "." indicates a decimal point symbol.
You're using the invariant culture; your culture is irrelevant to this. For this, the N4 format means
-d,ddd,ddd,ddd...
That is, possible leading negative sign indicator and commas between thousands groups. For details see: http://msdn.microsoft.com/en-us/library/dwhawy9k#NFormatString
You can look at
NegativeSign
NumberNegativePattern
NumberGroupSizes
NumberGroupSeparator
NumberDecimalSeparator
NumberDecimalDigits
for the invariant culture. If you do, you'll see:
-
1
{ 3 }
,
.
2
You are getting the comma because of "{0:N4}"
n ----- Number with commas for thousands ----- {0:n}
Source:
You will get the comma even without specifying InvariantCulture
Console.WriteLine(string.Format("{0:n4}", 1008.0));