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
Nick Malik [Microsoft] - 17 Jan 2006 11:23 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> 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.

Signature
--- 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.
--
Pieter - 17 Jan 2006 11:31 GMT
Ok! Thanks for the fast answer!
>> Hi,
>>
[quoted text clipped - 19 lines]
> In your app, move away from Single and use the Decimal type instead. That
> will convert to SqlDecimal more accurately.
Armin Zingler - 17 Jan 2006 12:04 GMT
> Hi,
>
[quoted text clipped - 8 lines]
> 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
Herfried K. Wagner [MVP] - 17 Jan 2006 12:24 GMT
"Pieter" <pietercoucke@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>

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