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 10 19448
<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
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
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
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
<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
<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
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
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 :) 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 :) 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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cindy liu |
last post by:
Hi,
In .Net, how to convert a string to a double?
Thanks in advance!
Cindy
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |