473,394 Members | 1,722 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,394 software developers and data experts.

BUG? SqlDecimal = Single/Double: 4.475 becomes 4.4749999999999996D ????

Hi,

I have a variable (m_sngPrixNetUnitaire) which is of type Single.
When I want to write the value to my SQL SERVER 2000, I put this value in my
DAL (MyDal.PrixNetUnitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUnitaire
=4.4749999999999996D...

Does anybody has any idea why this happens? Is this normal? Or is it a bug?
What should I do to have the exact value (4.475)?

Thanks a lot in advance,

Pieter
Jan 17 '06 #1
4 1608
"Pieter" <pi**********@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
Hi,

I have a variable (m_sngPrixNetUnitaire) which is of type Single.
When I want to write the value to my SQL SERVER 2000, I put this value in
my DAL (MyDal.PrixNetUnitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUnitaire
=4.4749999999999996D...

Does anybody has any idea why this happens? Is this normal? Or is it a
bug? What should I do to have the exact value (4.475)?

Thanks a lot in advance,

Pieter


Hi Peiter,

This is not a bug. See Jon's excellent write-up at:
http://www.yoda.arachsys.com/csharp/floatingpoint.html

In your app, move away from Single and use the Decimal type instead. That
will convert to SqlDecimal more accurately.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Jan 17 '06 #2
Ok! Thanks for the fast answer!

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:fM********************@comcast.com...
"Pieter" <pi**********@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP15.phx.gbl...
Hi,

I have a variable (m_sngPrixNetUnitaire) which is of type Single.
When I want to write the value to my SQL SERVER 2000, I put this value in
my DAL (MyDal.PrixNetUnitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUnitaire
=4.4749999999999996D...

Does anybody has any idea why this happens? Is this normal? Or is it a
bug? What should I do to have the exact value (4.475)?

Thanks a lot in advance,

Pieter


Hi Peiter,

This is not a bug. See Jon's excellent write-up at:
http://www.yoda.arachsys.com/csharp/floatingpoint.html

In your app, move away from Single and use the Decimal type instead. That
will convert to SqlDecimal more accurately.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--

Jan 17 '06 #3
"Pieter" <pi**********@hotmail.com> schrieb:
I have a variable (m_sngPrixNetUnitaire) which is of type Single.
When I want to write the value to my SQL SERVER 2000, I put this value in
my DAL (MyDal.PrixNetUnitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUnitaire
=4.4749999999999996D...


<URL:http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html>
<URL:http://www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html>
<URL:http://support.microsoft.com/?scid=kb;[LN];42980>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jan 17 '06 #4
"Pieter" <pi**********@hotmail.com> schrieb
Hi,

I have a variable (m_sngPrixNetUnitaire) which is of type Single.
When I want to write the value to my SQL SERVER 2000, I put this
value in my DAL (MyDal.PrixNetUnitaire), which is of datatype
SqlDecimal.

But during the conversion the value changes, and
MyDal.PrixNetUnitaire =4.4749999999999996D...

Does anybody has any idea why this happens? Is this normal? Or is it
a bug? What should I do to have the exact value (4.475)?

If you need a finite decimal representation of the value, use Decimal. Using
Single, you have a finite (4 bytes) binary representation, but the single
value you currently have, can not be converted to a /finite/ decimal
representation. Vice versa, the finite decimal value 4.475 can not be
converted to a finite binary value - that's what you come across now.

=> Declare m_sngPrixNetUnitaire as Decimal or as SqlDecimal (it's Value
property is also Decimal).
Armin

Jan 17 '06 #5

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

Similar topics

2
by: Simon Bunker | last post by:
I was just wondering why python doesn't make a distinction between single and double quotes - a bit like Perl does. Obviously I realise there are no dollar signs so you can't intrpolate a...
2
by: Diarmaid McGleenan | last post by:
Hi all. This isn't quite the same single/double-quote problem we see posted a multitude of times in this ng. Read on... I have a js function called ShowActiveLink which accepts a string...
5
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. ...
7
by: ma740988 | last post by:
I've got an unpacker that unpacks a 32 bit word into 3-10 bits samples. Bits 0 and 1 are dont cares. For the purposes of perfoming an FFT and an inverse FFT, I cast the 10 bit values into doubles....
6
by: G. | last post by:
This is an obvious bug in the String.Replace function: //load a XML string into a document XmlDocument doc = new XmlDocument(); doc.LoadXml("<test id='' />"); //Obtain the string...
4
by: Pieter | last post by:
Hi, I have a variable (m_sngPrixNetUnitaire) which is of type Single. When I want to write the value to my SQL SERVER 2000, I put this value in my DAL (MyDal.PrixNetUnitaire), which is of...
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
1
by: iaminsik | last post by:
Hi, I made a function prototype : <head> <script type="text/javascript"> function test (value) { alert (value); } </script> </head> Now, I hope to call it with quoted string like this :...
1
by: crimson08 | last post by:
does anyone know how to use radix sorting in a linked list???
1
by: eaocomp | last post by:
How do I trigger a single or double-click events when a user clicks a column within the gridview. I want the event to trigger and open a pop-up page related to the selected column. There are no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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,...
0
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...

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.