double.Parse and NumberFormat behavior

  • Thread starter Thread starter schaf
  • Start date Start date
S

schaf

Hi NG!
I have a little question about the localization of a double.

If I use the CultureInfo "en-US" I have the following behavior:

1.) If I enter the value 2.5 into a textbox the
double.Parse(text1.Text) returns 2.5 as expected.
2.) If I enter the value 2,5 into a textbox the
double.Parse(text1.Text) returns 25...why 25 and not 2500. The , is
the group-separator.

Is it possible to ignore these behavior to ensure the 2,5 and 2.5 will
always be interpreted as 2.5 9like in the calculator) ? Or do I have
to implement this behavior by my self?

Regards
Marcel
 
double.Parse(text1.Text) returns 25...why 25 and not 2500. The , is
the group-separator.

As I understand it, the group-separator is more for ouput an input; I
suspect it simply ignores it, which would explain 25.
Is it possible to ignore these behavior to ensure the 2,5 and 2.5 will
always be interpreted as 2.5 9like in the calculator) ?

Well, the calculator doesn't allow the group-separator *at all*,
always substituting for the decimal-separator. How would you interpret
"100,000"? 100? or 100k? Consider also that all number systems group
in thousands. In my view, it supports suitable locale behavior as it
stands...
Or do I have
to implement this behavior by my self?

Yes; but I wouldn't ;-p
 
schaf said:
I have a little question about the localization of a double.

If I use the CultureInfo "en-US" I have the following behavior:

1.) If I enter the value 2.5 into a textbox the
double.Parse(text1.Text) returns 2.5 as expected.
2.) If I enter the value 2,5 into a textbox the
double.Parse(text1.Text) returns 25...why 25 and not 2500. The , is
the group-separator.

Is it possible to ignore these behavior to ensure the 2,5 and 2.5 will
always be interpreted as 2.5 9like in the calculator) ? Or do I have
to implement this behavior by my self?

It is primitive but:

double.Parse(text1.Text.Replace(",", "."), new CultureInfo("en-US", false))

Arne
 
Back
Top