globalization / localization commas and periods
-
Hi, Can someone help me with this scenario please. I need to hard code the comparison of a double with the double value returned from a datatable. The user is using a language that has the comma as a decimal separator. double dbValue = if(dbValue < 2.1) then blah blah blah How do I hard code the "2.1" value or is there some way to globally have .net do this. Thanks,
-
Hi, Can someone help me with this scenario please. I need to hard code the comparison of a double with the double value returned from a datatable. The user is using a language that has the comma as a decimal separator. double dbValue = if(dbValue < 2.1) then blah blah blah How do I hard code the "2.1" value or is there some way to globally have .net do this. Thanks,
Glen Harvy wrote:
The user is using a language that has the comma as a decimal separator.
C# still uses period as decimal separator. A literal double value is always written as
2.1
in the code, regardless of any cultural settings.Despite everything, the person most likely to be fooling you next is yourself.
-
Glen Harvy wrote:
The user is using a language that has the comma as a decimal separator.
C# still uses period as decimal separator. A literal double value is always written as
2.1
in the code, regardless of any cultural settings.Despite everything, the person most likely to be fooling you next is yourself.
Thanks but it seems that my question may have been misleading. If the current culture is one that uses comma's as decimal separators, why does the following throw an error.. double x = ; // <-- no worries - returns 2,0 if(x > 2.1) <--- throws an error. double y = Double.Parse("2.1", System.Globalization.CultureInfo.InvariantCulture.NumberFormat); if(x > y) <---- will this fix above error????? Thanks,
Glen Harvy
-
Thanks but it seems that my question may have been misleading. If the current culture is one that uses comma's as decimal separators, why does the following throw an error.. double x = ; // <-- no worries - returns 2,0 if(x > 2.1) <--- throws an error. double y = Double.Parse("2.1", System.Globalization.CultureInfo.InvariantCulture.NumberFormat); if(x > y) <---- will this fix above error????? Thanks,
Glen Harvy
Glen Harvy wrote:
double x = ; // <-- no worries - returns 2,0
Where do you get the value from?
Glen Harvy wrote:
if(x > 2.1) <--- throws an error.
Why do you think that? There is nothing wrong with that line.
Despite everything, the person most likely to be fooling you next is yourself.
-
Glen Harvy wrote:
double x = ; // <-- no worries - returns 2,0
Where do you get the value from?
Glen Harvy wrote:
if(x > 2.1) <--- throws an error.
Why do you think that? There is nothing wrong with that line.
Despite everything, the person most likely to be fooling you next is yourself.
Sorry I put the source of x in brackets. x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .
Glen Harvy
-
Sorry I put the source of x in brackets. x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .
Glen Harvy
Irrespective of the culture settings, if you have a double, comparing it like
if(d > 2.1)
will work as expected*. Can you show some more code? * Not exactly, but for a different reason. You should instead useif((d - 2.1) < Double.Epsilon)
Cheers, Vikram.
The hands that help are holier than the lips that pray.
-
Sorry I put the source of x in brackets. x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .
Glen Harvy
Glen Harvy wrote:
x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .
No, as it's a double it's not formatted into something like "2,0". It's just a numierical value, and there isn't a decimal separator at all. It's first when you format a number into a string that a decimal separator character is used.
Despite everything, the person most likely to be fooling you next is yourself.
-
Glen Harvy wrote:
x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .
No, as it's a double it's not formatted into something like "2,0". It's just a numierical value, and there isn't a decimal separator at all. It's first when you format a number into a string that a decimal separator character is used.
Despite everything, the person most likely to be fooling you next is yourself.
OK... How is the "2.1" being converted to a numeric then? In other words, the hard coded part of the equation? I don't see how .Net can recognise it as a double because it hasn't got a "," in it. What's good for the goose must be good for the gander :-)
Glen Harvy
-
OK... How is the "2.1" being converted to a numeric then? In other words, the hard coded part of the equation? I don't see how .Net can recognise it as a double because it hasn't got a "," in it. What's good for the goose must be good for the gander :-)
Glen Harvy
Glen Harvy wrote:
I don't see how .Net can recognise it as a double because it hasn't got a "," in it.
The C# language always uses period for decimal separator, regardless of any culture settings. When you write a literal value in the code, it's always written the same.
Despite everything, the person most likely to be fooling you next is yourself.