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

P

Pieter

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
 
N

Nick Malik [Microsoft]

Pieter said:
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.
--
 
P

Pieter

Ok! Thanks for the fast answer!

Nick Malik said:
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.
 
A

Armin Zingler

Pieter said:
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
 
H

Herfried K. Wagner [MVP]

Pieter said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top