Convert.ToDouble() problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey


I am calling Convert.ToDouble(someString);

But this double can have to ways or representing doubles dependant on the
locale, it can be 1.0 or 1,0

Is there a way to make Convert.ToDouble(..) accept both comma and period
notations for representing numbers?

thanks
 
Hey


I am calling Convert.ToDouble(someString);

But this double can have to ways or representing doubles dependant on the
locale, it can be 1.0 or 1,0

Is there a way to make Convert.ToDouble(..) accept both comma and period
notations for representing numbers?

thanks

I think it is difficult. Example: "1,000" could be US notation for 1000
or european for 1.

If you know there are no thousands-separators, then you could do a Replace
on the string to change "," into "." (or the other way around)

Hans Kesting
 
We have one machine with comma notation and the other with period notation
and this must work on both and this is being called in lots of places.

Implementing my own Convert.ToDouble(..) seems to be the only easy way.

Is it possible to do this with IFormatProvider parameter in the ToDouble
method?
 
Hi,

Hey


I am calling Convert.ToDouble(someString);

But this double can have to ways or representing doubles dependant on the
locale, it can be 1.0 or 1,0

Is there a way to make Convert.ToDouble(..) accept both comma and period
notations for representing numbers?

As Hans pointed out, Convert.ToDouble can't know what the comma actually
means (thousands seperator or whole part/decimal part seperator) without
some extra help. The good news is that there is an overload to
Convert.ToDouble that takes culture into account. For example:

double someValue = Convert.ToDouble(someImput,
CultureInfo.CurrentCulture)

Although the above will use the user default culture (probably the right
choice if you are parsing user input), you can pass any culture you need to.

Regards,
Dan
 
What i did was to CreateNewCulture("En-Us") to ensure I use period notation
even on systems not configured that way.
 
What i did was to CreateNewCulture("En-Us") to ensure I use period notation
even on systems not configured that way.

Rather than use a new culture for that, use
CultureInfo.InvariantCulture.
 
What i did was to CreateNewCulture("En-Us") to ensure I use period notation
even on systems not configured that way.

What kind of input are you processing? If this is user-interactive input
I would strongly recommend you use CultureInfo.CurrentCulture. This will
meet your user's expectations. If you are dealing with non-user input
consider using CultureInfo.InvariantCulture. InvariantCulture is intended to
be universal/generic/culture-independant.

Regards,
Dan
 
Nope, its something we control, its just to be consistent across machine
regional settings that we cannot control.
 
Hey


I am calling Convert.ToDouble(someString);

But this double can have to ways or representing doubles dependant on the
locale, it can be 1.0 or 1,0

Is there a way to make Convert.ToDouble(..) accept both comma and period
notations for representing numbers?

thanks

I would think the simplest way of doing it would be to write a static
function that users Convert.ToDouble() and a helper function which
replaces all the "," with ".".

Or perhaps the right-most one if there are two digits after it,
otherwise replace it by a comma. (I'm assuming this is Euros or similar)

You have to deal with :-

3,45 as 3.45
3 as 3.00
3,456 as 3456.00
3,4 3, 3,8912 throw exceptions.

So err. delete all commas except the right most one. If that has three
digits after it, it's deleted. Two , it's replaced by a dp, anything
else throws an exception :)

Probably an idea to check the regionalisation as well. Haven't got that
far yet :)
 
What i did was to CreateNewCulture("En-Us") to ensure I use period notation
even on systems not configured that way.

Doesn't CreateNewCulture("En-Us") throw a DontBeSilly() exception ?

Sorry :)
 
Is there a way to make Convert.ToDouble(..) accept both comma and
period notations for representing numbers?

For culturally aware number conversions, use double.TryParse:

// the following could be from any culture supported the CLR
NumberFormat nfi = CultureInfo.CurrentCulture.NumberFormat;
double x;

if (double.TryParse("1,0", NumberStyles.Any, nfi, out x))
{
// the conversion succeded, x is now 1.0
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Back
Top