3

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?

6
  • 1
    I am getting 2050, not 20.50. Have you changed the settings of the UK regional settings in the control panel and set the decimal separator to ,? Commented Oct 1, 2012 at 11:08
  • My default settings in control panel are English (United Kingdom) and Decimal Symbol is . Commented Oct 1, 2012 at 11:49
  • Even if you get 2050, that is still incorrect as far as im concerned, it should throw it out as invalid format. Commented Oct 1, 2012 at 11:50
  • It may be incorrect as far as you are concerned, but that's that expected behaviour. What is the default culture of the account that this is running under? Has that account changed the settings? Commented Oct 1, 2012 at 11:53
  • I was mistaken, i do get 2050, but this still seems wrong to me. And it definatley is wrong for my users. Their in a GB locale and it shouldnt allow them to enter invalid figures. adding a comma in this location is invalid? Commented Oct 1, 2012 at 12:21

3 Answers 3

3

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 ,).

Sign up to request clarification or add additional context in comments.

1 Comment

I think the question is: Why is this code working? not how to fix it :)
1

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"))

Comments

0

From the updated question, I can tell that UK culture posiibly uses , character as a possible places separation character, like 2,000 standing for 2000, and 2,000,000 for 2000000. But it can also be like 2,0,0,0 and it still be 2000.

From that point of view there are 3 possible scenarios:

  1. you are always recieving data in a local format, then there is no problem, you just parse string as it is (however there is one gotcha - user can change string formatting options, so you better crate new default local culture).
  2. you are recieving data in predefined format, then you use some fixed culture to parse string.
  3. you are recieving data in unknown format relatively to local, i.e. you can receive both local and non-local culture formats. This is the worst case, and there is no generic solution to this. I suggest reducing this to case 2 or 1, depending this task is easier.

1 Comment

The point is, eventually i will be using the CurrentCulture or the UI culture rather than newing one so im not sure if this will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.