views:

1055

answers:

7

I have a textbox accepting user input; I am trying to use this user input it to populate this member of one of my business objects:

public System.Decimal? ExchangeRate
The application is localized - I need to support at the same time cultures that accept these as valid inputs: "1,5" and "1.5" The code I have now is:
var culture = Thread.CurrentThread.CurrentUICulture;

int exchangeRate;
int.TryParse(txtExchangeRate.Text, NumberStyles.Number, culture, 
    out exchangeRate);

entity.ExchangeRate = exchangeRate;

When the user culture is set to a culture that expects the "1,5" format (comma as decimal separator) - e.g "ro-RO", I want the value that gets stored in entity.ExchangeRate to be 1.5; however, when running the code above, it gets converted to 15 instead.

Any suggestions on how to convert these various formats so that the data that gets stored in my business entity is "1.5" (point as decimal separator)?

Thanks.

A: 

You should probably be using CurrentCulture (as opposed to CurrentUICulture) for localization (e.g. date/number formatting).

dommer
Nope, I still get the same results when using CurrentCulture ("35" instead of "3.5").
A: 

FYI I know this isn't your problem, but its a pointer for other people who might be doing this:

When you set your culture, you can't have your application be able to handle input of different cultures. It must be of the type that you have specified.

Therefore, if you set ro-RO as the culture, it won't understand both 1.5 and 1,5 as the same thing.

ck
+1  A: 
  1. Obviously, int cannot hold 1.5 ! :-) Use float instead.

  2. Use CurrentCulture instead of CurrentUICulture. My culture is fr-BE (therefore accepts 1,5 but my Windows UI is English, which doesn't).

  3. I would make the float.Parse() test with both CurrentCulture AND InvariantCulture: By the time some programs learned to accept "1,5", everybody was used to type "1.5". There's nothing which bothers me more than Excel requiring me to type 1,5 when I say 1.5 ! Also, here in Belgium, the 1st year government launched the web-based tax declaration, the site forced you to use commas instead of periods as decimal points. Everybody was wondering why the hell the figures entered were refused! So be nice to your users and accept both.

Serge - appTranslator
A: 

You guys were right - it made sense to use Thread.CurrentThread.CurrentCulture instead of Thread.CurrentThread.CurrentUICulture and decimal.TryParse instead of int.TryParse.

But these changes would still not solve my problem. And after playing around with the code some more, I can now simplify the issue to this:

I am using a telerik RadNumericTextBox control which enforce users to use the correct format based on their culture. So, when Thread.CurrentThread.CurrentCulture is set to "ro-RO", it will only accept the "1,5" format, and when it's set to "en-GB", it will only accept the "1.5" format.

Here's the code I am using now:

decimal exchangeRate;
decimal.TryParse(txtExchangeRate.Text, out exchangeRate);
entity.ExchangeRate = exchangeRate;

Case 1: current culture is "en-GB" - accepted input is "1.5" , exchangeRate is set to 1.5 - everything works fine.

Case 2: current culture is "ro-RO" - accepted input is "1,5" , but after executing the decimal.TryParse... line, exchangeRate is set to 15 - wrong, obviously. I should also mention that in this case, the value of txtExchangeRate.Text is also shown as "1.5" in my Watch window.

So, it looks like decimal.TryParse will take into consideration the current culture, but I can't find a way to actually make it work properly for me. Any suggestions?

A: 

OK, here's the code that seems to work on both cases I described in my above post (1. culture "ro-RO", comma as decimal separator and 2. culture "en-GB", dot as decimal separator):

decimal exchangeRate;
decimal.TryParse(txtExchangeRate.Text, NumberStyles.Any, 
       CultureInfo.InvariantCulture, out exchangeRate);
entity.ExchangeRate = exchangeRate;
A: 

Thanks!!

This worked for me too

Convert.ToDecimal("0.05",CultureInfo.InvariantCulture);

returned 0,05 for "fr-BE"

A: 

Be sure to read this article. Does your code pass the the turkey test

Andrew Cates