473,398 Members | 2,404 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,398 software developers and data experts.

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 1678
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(sVal, Globalization.NumberFormatInfo.InvariantInfo)


"AGP" <si**********@softhome.netwrote in message
news:qD******************@nlpi067.nbdc.sbc.com...
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**********@softhome.netschreef in bericht
news:qD******************@nlpi067.nbdc.sbc.com...
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**************@TK2MSFTNGP06.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**********@softhome.netschreef in bericht
news:qD******************@nlpi067.nbdc.sbc.com...
>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 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(sVal, Globalization.NumberStyles.Float)

and

Double.Parse(sVal, Globalization.NumberFormatInfo.InvariantInfo)

AGP

"Bill McCarthy" <Bi**@localhost.comwrote in message
news:78**********************************@microsof t.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(sVal, Globalization.NumberFormatInfo.InvariantInfo)


"AGP" <si**********@softhome.netwrote in message
news:qD******************@nlpi067.nbdc.sbc.com...
>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 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
NumberFormatInfo, but you won't get a complete list of those classes in
the intellisense.
so what is the difference between
Double.Parse(sVal, Globalization.NumberStyles.Float)

and

Double.Parse(sVal, Globalization.NumberFormatInfo.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(sVal2, Globalization.NumberFormatInfo.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(sVal2,
Globalization.NumberFormatInfo.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.comwrote in message
news:e9**************@TK2MSFTNGP02.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
NumberFormatInfo, but you won't get a complete list of those classes in
the intellisense.
>so what is the difference between
Double.Parse(sVal, Globalization.NumberStyles.Float)

and

Double.Parse(sVal, Globalization.NumberFormatInfo.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**********@softhome.netschreef in bericht
news:Ku*******************@nlpi064.nbdc.sbc.com...
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**************@TK2MSFTNGP06.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**********@softhome.netschreef in bericht
news:qD******************@nlpi067.nbdc.sbc.com. ..
>>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 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**********@softhome.netwrote in message
news:YE******************@flpi147.ffdc.sbc.com...
ok here is what i have

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

b = Double.Parse(sVal2, Globalization.NumberFormatInfo.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(sVal2,
Globalization.NumberFormatInfo.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.comwrote in message
news:e9**************@TK2MSFTNGP02.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
NumberFormatInfo, but you won't get a complete list of those classes in
the intellisense.
>>so what is the difference between
Double.Parse(sVal, Globalization.NumberStyles.Float)

and

Double.Parse(sVal, Globalization.NumberFormatInfo.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
by: Steven C. | last post by:
string strS; stringstream stmT; double dR; stmT << strS; stmT >> dR; Is this the best way?
1
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...
1
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...
5
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". ...
8
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...
5
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...
6
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...
5
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.