Link to home
Create AccountLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

asked on

culture fr-CA cause an error in currency

have a web site in ASP.NET 2.0 with C#.
I have the following command:
 
            string minpStr = String.Format("{0:C}", decimal.Parse(lblMin.Text));
which return the price with a currency sign.
If I change the culture to fr-CA for Canada french I get an error:
Input string was not in a correct format.
Why is that?
Thankyou
Anat
Avatar of nathana21
nathana21
Flag of United States of America image

http://www.thescripts.com/forum/thread274181.html

This article addresses this...
Avatar of ANAT2403

ASKER

Hi,
I found the answer:
I wrote 678.45 and I wanted to see this amount with the currency sign.
but in french canada culture this number has to appear like 678,45.
So this is why I get Input string was not in a correct format.
Anat
but still the question is if I have string x = "345.45" and I want to convert it to a decimal or double number and I am using fr-CA culture. How do I convert it? I tried what you wrote in the article
decimal num = decimal.Parse(temp, NumberStyles.Number, MyCltr.NumberFormat);
but it did not help.
Anat
ASKER CERTIFIED SOLUTION
Avatar of Velio
Velio
Flag of South Africa image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Göran Andersson
To parse numbers with a decimal point, you can use the invariant culture:

decimal.Parse(lblMin.Text, CultureInfo.InvariantCulture)
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
> Are you setting the culture from the user's browser settings?

That has no direct effect at all on the code running on the server. The browser sends the information to the server, but you have to specifially pick up the information, create a CultureInfo object from it, and use that object. Otherwise it has no effect at all.
Green,

"but you have to specifially pick up the information, create a CultureInfo object "

Well that is what I meant by "Are you setting the culture from the user's browser settings?"

"Setting the culture" would mean setting the applications culture from the information from the user's browser setting.

Doing this would mean that your app is set to the users culture and therefore would know how to handle inputs accordingly.

At least that's what current info applies - there is little or now information at all on "How to handle user inputs from different cultures" - I would be interested myself if someone could find something as such.

regards.
Let's make some order:
I override the initialize culture and set the culture to be fr-CA.
I expect that the user will enter the sums in  the fr-CA culture which iwill be 344,55.
Now suppose there was a mistake and the user entered 344.55.
If I set the command     string minpStr = String.Format("{0:C}", decimal.Parse(lblMin.Text));
I will get an error in the decimal.parse. So how should I handle this? How shall I check that it was entered in the correct format?
Anat
as per GreenGhost's suggestion (I feel this is the best way to go):
String.Format("{0:C}", Decimal.Parse(lblMin.Text, Globalization.CultureInfo.InvariantCulture))

as per my suggestion:
String.Format("{0:C}", Decimal.Parse(lblMin.Text.Replace(".",",")))
If it is an option, you could also pre-empt the problem by not allowing the user to enter the comma or fullstop in your User Interface.

regards.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account