Hi all:
I am trying to parse numeric input a user provides on a web site.
I am having a problem with dollar amounts. The following command results in an OverflowException:
int.Parse("77,500.50", NumberStyles.Currency, CultureInfo.CurrentCulture)
but all of the following commands do not:
int.Parse("77,500.00", NumberStyles.Currency, CultureInfo.CurrentCulture)
int.Parse("77,50050", NumberStyles.Currency, CultureInfo.CurrentCulture)
decimal.Parse("77,500.50", NumberStyles.Currency, CultureInfo.CurrentCulture)
Note: the current culture is "en-US".
I understand that the integer can't hold the decimal portion of the number, but according to the documentation (.NET 2.0), an OverflowException is thrown if
"s represents a number less than MinValue or greater than MaxValue"
The resulting number is obviously not too large or small to fit in an integer.
I can work around this, but I think that the Framework should handle this situation better.
Thanks,
SA.

int.Parse oddity
Amy Dullard - MSFT
A cast is, by definition, changing the type of the object. tryparse is checking to see if a string contains the specific object in question. It seemed odd to me that this would work when I first read it, and since David has responded, and I've considered his reply, I would tend to agree. The docs must be wrong. You should use double.tryparse if the number could be floating point, then you can cast to int.
Plenty
Robert McDaniel
Integers don't have a fractional part. It's entirely understandable (and correct, IMHO) that the int.Parse method does not allow a decimal point in the textual representation of an integer. However, it should throw a FormatException, not an OverflowException.
The OverflowException seems to be specific to the Currency NumberStyle, as most other variations do throw the correct exception.
CindyG
David:
Yes, that's exactly the behavior I expected!
I want "$2.99" to become 2, and not 3 or not 2.99. I am looking for truncation in this case, which is why first casting to a decimal amount doesn't make much sense to me. I would gladly do that, if I was after the full dollar amount.
Thanks for your replies though,
Sven.
Marc Konchar
David:
Thanks for your reply.
I am not sure I think that's logical. My workaround is to parse the string as a decimal value and then cast it, as in:
int ivalue = (int)dvalue;
I would expect the parser to be even more tolerant of precision than the cast operation
SA.
asdfj
Are you sure you want to place a dollar amount into a int That's what the decimal type is for. Someone could enter an amount of $2.99 which after the cast would be $2.00.
It should be something you should be aware of.
Animas
See my previous post... I am using that particular workaround... But parsing is something very specific: you have a string representation of something (a number in my case), in a somewhat known format (a dollar amount in my case) that you want to see represented as that particular something.
If I can parse this to int
"77,50050"
(a string whose formatting isn't even a valid number)
why can't I parse this
"77,500.50"
just because of the decimal point...
Anyway, it's probably time to close this discussion. I have a working solution, so end of story I guess.
Thanks to everyone for their input,
SA.
Brian Jimdar2
Math.Floor is what you're after then.
bashok
The .NET 2.0 Class Lib reference
http://msdn2.microsoft.com/en-us/library/9yaffedz.aspx
SA.
Juozas Kimtys
int value = (int)Decimal.Parse(...);
Slava Imeshev
cgraus:
Yes, but that wasn't the issue. The issue was that the documentation seems to indicate that the int32.Parse method can handle "$77,500.50", and it can't. It throws an overflow exception, which is an odd way of responding to that condition.
SA.
MC Wu
On the page listed I can't find anywhere that would give the indication that it can handle handle decimal numbers. Can you post paragraph caused you to make this assumption (so I can see if we can improve the docs)
Michael Hinkel
Does the currency style allow for the comma
JarleS