473,554 Members | 4,465 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 19601

<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
47053
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
4
3512
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
9716
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
11012
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...
2
6607
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
8725
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
2328
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...
4
2558
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
7781
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. ...
1
7538
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...
0
7869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6121
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...
1
5421
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...
0
3543
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...
0
3531
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
817
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...

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.