473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coercion of a String Into a Double Doesnt work (??!!)

AGP
I've been scratching my head for weeks to understand why some code doesnt
work for me.
here is what i have:

dim sVal as string = "13.2401516 "
dim x as double

x = sVal

debug.writeline ( x)

Now on my system (English WinXp proSP3) the debug line
is as follows
13.2401516

However, when a user in a different locale (different localized version of
Windows) the debug line prints
132401516
The same number only without the decimal. Can anyone shed some light on why
this happanes? is it because the user may have a

different format for numbers and the coercion is done according to that
format? How can i ensure that i get the proper conversion?

AGP
Aug 25 '08 #1
9 1700
Hi AGP,

The reason is in some locales the . is used as a thousands separator much
like how , is used in others, e.g 1,000 is the same as 1.000 depending on
the locale.

To take explicit control of how the Double is parsed used the double Parse
method, e.g:

x = Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)


"AGP" <si**********@s ofthome.netwrot e in message
news:qD******** **********@nlpi 067.nbdc.sbc.co m...
I've been scratching my head for weeks to understand why some code doesnt
work for me.
here is what i have:

dim sVal as string = "13.2401516 "
dim x as double

x = sVal

debug.writeline ( x)

Now on my system (English WinXp proSP3) the debug line
is as follows
13.2401516

However, when a user in a different locale (different localized version of
Windows) the debug line prints
132401516
The same number only without the decimal. Can anyone shed some light on
why this happanes? is it because the user may have a

different format for numbers and the coercion is done according to that
format? How can i ensure that i get the proper conversion?

AGP

Aug 25 '08 #2
AGP,

Be aware that you should avoid hard coded decimimal pointers

However, that is impossiblie in a document or a webpage.

Have therefore a look at this.
http://www.vb-tips.com/Cultures.aspx

Cor

"AGP" <si**********@s ofthome.netschr eef in bericht
news:qD******** **********@nlpi 067.nbdc.sbc.co m...
I've been scratching my head for weeks to understand why some code doesnt
work for me.
here is what i have:

dim sVal as string = "13.2401516 "
dim x as double

x = sVal

debug.writeline ( x)

Now on my system (English WinXp proSP3) the debug line
is as follows
13.2401516

However, when a user in a different locale (different localized version of
Windows) the debug line prints
132401516
The same number only without the decimal. Can anyone shed some light on
why this happanes? is it because the user may have a

different format for numbers and the coercion is done according to that
format? How can i ensure that i get the proper conversion?

AGP


Aug 25 '08 #3
AGP
The data that i am reading will be non-locale specific and will always be
written
as pure decimal. Example 13.58 is read as 13 and 58 hundreths. So if I read
your link properly then I should just coerce the string before assiging it
and that should
give me a pure decimal number as follows:

dim sVal as string = "13.2401516 "
dim x as double
x = CDbl( sVal )

AGP

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:uN******** ******@TK2MSFTN GP06.phx.gbl...
AGP,

Be aware that you should avoid hard coded decimimal pointers

However, that is impossiblie in a document or a webpage.

Have therefore a look at this.
http://www.vb-tips.com/Cultures.aspx

Cor

"AGP" <si**********@s ofthome.netschr eef in bericht
news:qD******** **********@nlpi 067.nbdc.sbc.co m...
>I've been scratching my head for weeks to understand why some code doesnt
work for me.
here is what i have:

dim sVal as string = "13.2401516 "
dim x as double

x = sVal

debug.writelin e ( x)

Now on my system (English WinXp proSP3) the debug line
is as follows
13.2401516

However, when a user in a different locale (different localized version
of Windows) the debug line prints
132401516
The same number only without the decimal. Can anyone shed some light on
why this happanes? is it because the user may have a

different format for numbers and the coercion is done according to that
format? How can i ensure that i get the proper conversion?

AGP



Aug 26 '08 #4
AGP
Im using VS2005 and noticed that secondary option doesnt come up by default
so what is the difference between
Double.Parse(sV al, Globalization.N umberStyles.Flo at)

and

Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)

AGP

"Bill McCarthy" <Bi**@localhost .comwrote in message
news:78******** *************** ***********@mic rosoft.com...
Hi AGP,

The reason is in some locales the . is used as a thousands separator much
like how , is used in others, e.g 1,000 is the same as 1.000 depending on
the locale.

To take explicit control of how the Double is parsed used the double Parse
method, e.g:

x = Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)


"AGP" <si**********@s ofthome.netwrot e in message
news:qD******** **********@nlpi 067.nbdc.sbc.co m...
>I've been scratching my head for weeks to understand why some code doesnt
work for me.
here is what i have:

dim sVal as string = "13.2401516 "
dim x as double

x = sVal

debug.writelin e ( x)

Now on my system (English WinXp proSP3) the debug line
is as follows
13.2401516

However, when a user in a different locale (different localized version
of Windows) the debug line prints
132401516
The same number only without the decimal. Can anyone shed some light on
why this happanes? is it because the user may have a

different format for numbers and the coercion is done according to that
format? How can i ensure that i get the proper conversion?

AGP


Aug 26 '08 #5
AGP wrote:
Im using VS2005 and noticed that secondary option doesnt come up by default
The double.Parse method has an overload that takes an IFormatProvider .
You can use any class that implements that interface, including
NumberFormatInf o, but you won't get a complete list of those classes in
the intellisense.
so what is the difference between
Double.Parse(sV al, Globalization.N umberStyles.Flo at)

and

Double.Parse(sV al, Globalization.N umberFormatInfo .InvariantInfo)

AGP
The difference is that the first one still uses the default culture,
which may not use a period as decimal separator. The InvariantInfo is
culture independent and a period has been chosen as it's decimal separator.

--
Göran Andersson
_____
http://www.guffa.com
Aug 26 '08 #6
AGP
ok here is what i have

dim sVal1 as string = "13.2401516 "
dim sVal2 as string = "53.2168183 "
a = CDbl(sVal1)

b = Double.Parse(sV al2, Globalization.N umberFormatInfo .InvariantInfo)

and the debug line from the user

132401516

53,2168183

He is using a German Windows XP version. In that locale the number 1300 and
24/100 would be written 1300,24 so it seems to me that the second is the
correct way to writre it as suggested. Double.Parse(sV al2,
Globalization.N umberFormatInfo .InvariantInfo) seems to correctly interpret
the number. But my question is...that is how the user sees the number that
is calculated but internally is it still 53 and 2168183/10000000? In other
words, if i pump that value to another process will it still be seen as 53
and 2168183/10000000?

Im going to install the German MUI and do some testing here but i think Im
learning something that I never paid much attention to in the past.

AGP
"Göran Andersson" <gu***@guffa.co mwrote in message
news:e9******** ******@TK2MSFTN GP02.phx.gbl...
AGP wrote:
>Im using VS2005 and noticed that secondary option doesnt come up by
default

The double.Parse method has an overload that takes an IFormatProvider . You
can use any class that implements that interface, including
NumberFormatInf o, but you won't get a complete list of those classes in
the intellisense.
>so what is the difference between
Double.Parse(s Val, Globalization.N umberStyles.Flo at)

and

Double.Parse(s Val, Globalization.N umberFormatInfo .InvariantInfo)

AGP

The difference is that the first one still uses the default culture, which
may not use a period as decimal separator. The InvariantInfo is culture
independent and a period has been chosen as it's decimal separator.

--
Göran Andersson
_____
http://www.guffa.com

Aug 27 '08 #7
Pure,

Pure decimal, only 2% of the world population is using the dot as decimal
seperator, the rest is using a comma.
Even in at least one country where English is used is the comma the decimal
seperator.

Cor

"AGP" <si**********@s ofthome.netschr eef in bericht
news:Ku******** ***********@nlp i064.nbdc.sbc.c om...
The data that i am reading will be non-locale specific and will always be
written
as pure decimal. Example 13.58 is read as 13 and 58 hundreths. So if I
read
your link properly then I should just coerce the string before assiging it
and that should
give me a pure decimal number as follows:

dim sVal as string = "13.2401516 "
dim x as double
x = CDbl( sVal )

AGP

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:uN******** ******@TK2MSFTN GP06.phx.gbl...
>AGP,

Be aware that you should avoid hard coded decimimal pointers

However, that is impossiblie in a document or a webpage.

Have therefore a look at this.
http://www.vb-tips.com/Cultures.aspx

Cor

"AGP" <si**********@s ofthome.netschr eef in bericht
news:qD******* ***********@nlp i067.nbdc.sbc.c om...
>>I've been scratching my head for weeks to understand why some code
doesnt work for me.
here is what i have:

dim sVal as string = "13.2401516 "
dim x as double

x = sVal

debug.writeli ne ( x)

Now on my system (English WinXp proSP3) the debug line
is as follows
13.2401516

However, when a user in a different locale (different localized version
of Windows) the debug line prints
132401516
The same number only without the decimal. Can anyone shed some light on
why this happanes? is it because the user may have a

different format for numbers and the coercion is done according to that
format? How can i ensure that i get the proper conversion?

AGP



Aug 27 '08 #8
Cor Ligthert[MVP] wrote:
Pure decimal, only 2% of the world population is using the dot as
decimal seperator, the rest is using a comma.
Cite? I present to you the countries of China and India, which I suggest
between them make up somewhat more than 2% of the world's population ;-)

http://en.wikipedia.org/wiki/Decimal_separator

Andrew
Aug 27 '08 #9
Suggest y'all use TryParse() rather than Parse(). The former will return a
zero value if the input is bad whereas TryParse() will return a zero value
but not throw an exception.

-bwr-

"AGP" <si**********@s ofthome.netwrot e in message
news:YE******** **********@flpi 147.ffdc.sbc.co m...
ok here is what i have

dim sVal1 as string = "13.2401516 "
dim sVal2 as string = "53.2168183 "
a = CDbl(sVal1)

b = Double.Parse(sV al2, Globalization.N umberFormatInfo .InvariantInfo)

and the debug line from the user

132401516

53,2168183

He is using a German Windows XP version. In that locale the number 1300
and 24/100 would be written 1300,24 so it seems to me that the second is
the correct way to writre it as suggested. Double.Parse(sV al2,
Globalization.N umberFormatInfo .InvariantInfo) seems to correctly interpret
the number. But my question is...that is how the user sees the number that
is calculated but internally is it still 53 and 2168183/10000000? In other
words, if i pump that value to another process will it still be seen as 53
and 2168183/10000000?

Im going to install the German MUI and do some testing here but i think Im
learning something that I never paid much attention to in the past.

AGP
"Göran Andersson" <gu***@guffa.co mwrote in message
news:e9******** ******@TK2MSFTN GP02.phx.gbl...
>AGP wrote:
>>Im using VS2005 and noticed that secondary option doesnt come up by
default

The double.Parse method has an overload that takes an IFormatProvider .
You can use any class that implements that interface, including
NumberFormatIn fo, but you won't get a complete list of those classes in
the intellisense.
>>so what is the difference between
Double.Parse( sVal, Globalization.N umberStyles.Flo at)

and

Double.Parse( sVal, Globalization.N umberFormatInfo .InvariantInfo)

AGP

The difference is that the first one still uses the default culture,
which may not use a period as decimal separator. The InvariantInfo is
culture independent and a period has been chosen as it's decimal
separator.

--
Göran Andersson
_____
http://www.guffa.com


Aug 29 '08 #10

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

Similar topics

8
14847
by: Steven C. | last post by:
string strS; stringstream stmT; double dR; stmT << strS; stmT >> dR; Is this the best way?
1
1838
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was not added. Please comment. Neil PEP: 349 Title: Generalised String Coercion
1
1737
by: ziga.seilnacht | last post by:
""" I am trying to write some classes representing the quaternion number. I wrote a base class, which implements only the numerical interface, and a few subclasses, which provide methods for their specific domain. Since the operator methods will be the same for all these classes, the base class operator methods don't explicitly return an instance of this base class, but rather an instance of the class that called them (ie 'return...
5
3888
by: raffelm | last post by:
I'm struggling to find a way to include long path names in a command line argument string that I have to build at runtime. I need to create a string like -o:"c:\my documents\my file.txt". Everything I have tried so far causes the program I am calling to fail (I know it can accept long file names as I have tried it from the cmdline). The problem I am having is using the Format command and getting "
8
10699
by: temp | last post by:
Hi, completeString = "<p><a href="http://www.google.com"> ..... " Now I want to extract the link pointed out by href so I try to find the position of first the href tag and then try to see where the quotes begin. pos_tag = completeString.IndexOf("href", 0, completeString.Length); pos_text_tag_start = completeString.IndexOf("\"", pos_tag,
5
1472
by: Chris H | last post by:
Okay, I am trying to us a varialble with a list of number values as my data for an array, yet when I do this it doesnt return any values from the array, i was able to temporarily fix the proble by just doing s normal array setup in my function, but the problem is I want the ability to have teh array values to be changed easily in a varibale style config/include file... Here is the code i am working with... ...
6
34152
by: compboy | last post by:
Can anyone help me about this. I have been trying all the ways I knew and I could find but just didnt work. I have tried: using itoa but it says that it doesnt have that function. and also
5
1372
by: orfiyus | last post by:
Hi I am having a problem with a query. I am trying to query my oracle database for a certain number corresponding to at least 4 other fields of information. Anyway I dont know what is going on but the query doesnt work unless I pull out the part of the clause which contains a string. All other where clauses contain integers stored into php variables but for some reason it doesnt like the string. I would appreciate if someone could take a...
0
1011
by: mazerj2006 | last post by:
I'm an old hand at python, but totally new to win32com -- I've run into a snag that seems to be related to python's (usually handy) lack of distinction between floats and doubles. Can anyone explain to me (or point me towards the right docs) how the win32com module handles an ActiveX component that returns or expects an array of SINGLE precision floats (ie, 32-bit floats)? I'm using a vendor-supplied ActiveX component with two related...
0
8889
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
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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...
0
9116
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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
6702
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
6011
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
4519
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...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.