Home All Groups Group Topic Archive Search About

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

Author
17 Jan 2006 10:18 AM
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

Author
17 Jan 2006 11:26 AM
Nick Malik [Microsoft]
Show quote
"Pieter" <pietercou***@hotmail.com> wrote in message
news:uTFEg90GGHA.3896@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.
--
Author
17 Jan 2006 11:31 AM
Pieter
Ok! Thanks for the fast answer!

Show quote
"Nick Malik [Microsoft]" <nickmalik@hotmail.nospam.com> wrote in message
news:fMSdnbdUsf7sTlHeRVn-tg@comcast.com...
> "Pieter" <pietercou***@hotmail.com> wrote in message
> news:uTFEg90GGHA.3896@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.
> --
>
>
Author
17 Jan 2006 12:04 PM
Armin Zingler
Show quote
"Pieter" <pietercou***@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
Author
17 Jan 2006 12:24 PM
Herfried K. Wagner [MVP]
"Pieter" <pietercou***@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/>

AddThis Social Bookmark Button