473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 19553

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
4
by: Irepan | last post by:
does anybody know how to change the global IFormatProvider of my project so everycall to Convert.ToDouble on it uses this format provider instead of the one the one on the computer's regional...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: user | last post by:
Hello i have problems with this function, always thru exception for all data, for example: Convert.ToDouble("545.0"). Why ? I want to convert string data in different format into double, i have...
4
by: Daniel Walzenbach | last post by:
Hi, I wonder if somebody could explain me the difference between Double.Parse and Convert.ToDouble. If I'm not mistaken they are implemented differently (I though for a moment they might be the same...
2
by: Jason | last post by:
In VB.NET, when I use System.Convert.ToDouble(string Val) to convert a string variable to double variable, I got something interesting: Dim stringVal As String = "101.01" Dim doubleVal As Double...
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
2
by: trondhuso | last post by:
Hi group, I've found some code that I want to use in a project that I am working on, but the code is for c# .net and not 2003 or 2005 that I have available. In this code the program yells on...
4
by: Yoavo | last post by:
Hi, I want to convert a string to double. I use the function: "System.Convert.ToDouble". The problem is that if the string contains the character "." the program aborts. What might be the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.