473,888 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert.ToDoubl e() problem

Hey
I am calling Convert.ToDoubl e(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.ToDoubl e(..) accept both comma and period
notations for representing numbers?

thanks
Nov 15 '05 #1
10 19687

<di********@dis cussion.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hey
I am calling Convert.ToDoubl e(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.ToDoubl e(..) 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.ToDoubl e(..) 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******** ******@TK2MSFTN GP12.phx.gbl...

<di********@dis cussion.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hey
I am calling Convert.ToDoubl e(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.ToDoubl e(..) 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********@dis cussion.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hey
I am calling Convert.ToDoubl e(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.ToDoubl e(..) accept both comma and period notations for representing numbers?


As Hans pointed out, Convert.ToDoubl e 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.ToDoubl e that takes culture into account. For example:

double someValue = Convert.ToDoubl e(someImput,
CultureInfo.Cur rentCulture)

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 CreateNewCultur e("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******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

<di********@dis cussion.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hey
I am calling Convert.ToDoubl e(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.ToDoubl e(..) accept both comma and

period
notations for representing numbers?


As Hans pointed out, Convert.ToDoubl e 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.ToDoubl e that takes culture into account. For example:

double someValue = Convert.ToDoubl e(someImput,
CultureInfo.Cur rentCulture)

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********@dis cussion.microso ft.com> wrote:
What i did was to CreateNewCultur e("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.Inv ariantCulture.

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

<di********@dis cussion.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
What i did was to CreateNewCultur e("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.Cur rentCulture. This will
meet your user's expectations. If you are dealing with non-user input
consider using CultureInfo.Inv ariantCulture. InvariantCultur e 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******** ******@tk2msftn gp13.phx.gbl...

<di********@dis cussion.microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
What i did was to CreateNewCultur e("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.Cur rentCulture. This will
meet your user's expectations. If you are dealing with non-user input
consider using CultureInfo.Inv ariantCulture. InvariantCultur e is intended to be universal/generic/culture-independant.

Regards,
Dan

Nov 15 '05 #8
di********@disc ussion.microsof t.com wrote:
Hey
I am calling Convert.ToDoubl e(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.ToDoubl e(..) 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.ToDoubl e() 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********@disc ussion.microsof t.com wrote:
What i did was to CreateNewCultur e("En-Us") to ensure I use period notation
even on systems not configured that way.


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

Sorry :)

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
47079
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
4
3544
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 configuration
4
9784
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 numeric value according to an arbitrary regular expression.
3
688
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 formats like: 37.439351 2.00000e+02 How can i do it most easily ?
4
11157
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 like cint(anInt) and cType(anInt, System.Int32) but I checked with ildasm) - if I didn't made a mistake. So when to use which syntax? Is there any performance penalty when using the one over the other or does anybody knows any differences? '...
2
6638
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 doubleVal = System.Convert.ToDouble(stringVal) Msgbox (doubleVal) will return 101.01 but...... Dim stringVal As String = "101.00"
3
8758
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; this produced an error
2
2342
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 (float)Convert.ToDouble(xml.value) (which in this case is "8.25"). I am wondering if someone could tell me how the command is changed in 2003 or 2005 as it stops here.
4
2570
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 problem ? Yoav.
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10882
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4243
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3251
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.