Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Decimal.Parse comma problems

Tags:

c#

I need to parse a string to a decimal and it might be used in the UK or a european culture.

If in a european culture 2,00 would be 2

However, 2,00 would be invalid in the UK culture as a comma denotes a thousand.

If i use

Double.Parse("20,50", new CultureInfo("en-GB")) 

it returns 2050

I would have expected this to throw an invalid exception.

Can anyone help?

like image 781
sianabanana Avatar asked Dec 05 '25 14:12

sianabanana


2 Answers

You should not be getting an exception, as , is a valid character in the parse string (as you said, it is interpreted as the thousands separator).

However, I am seeing 2050 for the same piece of code, not 20.50.

I would expect to see your result for the en-GB culture only if those have been manually changed in the control panel for that region (so if someone set the decimal separator to , and the thousands separator to something that is not ,).

like image 129
Oded Avatar answered Dec 12 '25 17:12

Oded


The point is that "2,00" in GB culutre should be invalid but it parses it like it was using a european culture.

I want it to throw an exception.

In contrast - Double.Parse("20.50", new CultureInfo("fr-FR")) throws an exception as the decimal place is invalid.

So why isnt this true for Double.Parse("20,50", new CultureInfo("en-GB"))

like image 36
sianabanana Avatar answered Dec 12 '25 17:12

sianabanana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!