468,771 Members | 1,962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Convert.ToDouble() problem

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
Nov 15 '05 #1
10 19166

<di********@discussion.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
Nov 15 '05 #2

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?
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:ep**************@TK2MSFTNGP12.phx.gbl...

<di********@discussion.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #3
Hi,

<di********@discussion.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
Nov 15 '05 #4
What i did was to CreateNewCulture("En-Us") to ensure I use period notation
even on systems not configured that way.

"Daniel Pratt" <ko******************@hotmail.com> wrote in message
news:uZ**************@TK2MSFTNGP10.phx.gbl...
Hi,

<di********@discussion.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #5
<di********@discussion.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

<di********@discussion.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
Nov 15 '05 #7
Nope, its something we control, its just to be consistent across machine
regional settings that we cannot control.
"Daniel Pratt" <ko******************@hotmail.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...

<di********@discussion.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #8
di********@discussion.microsoft.com wrote:
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 :)

Nov 15 '05 #9
di********@discussion.microsoft.com wrote:
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 :)

Nov 15 '05 #10
di********@discussion.microsoft.com wrote:
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)
Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by cindy liu | last post: by
4 posts views Thread by Irepan | last post: by
3 posts views Thread by user | last post: by
2 posts views Thread by Jason | last post: by
3 posts views Thread by mrajanikrishna | last post: by
2 posts views Thread by trondhuso | last post: by
4 posts views Thread by Yoavo | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.