|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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
Show quote
"Pieter" <pietercou***@hotmail.com> wrote in message Hi Peiter,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 > 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. -- 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. > -- > >
Show quote
"Pieter" <pietercou***@hotmail.com> schrieb If you need a finite decimal representation of the value, use Decimal. Using> 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)? 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 "Pieter" <pietercou***@hotmail.com> schrieb: <URL:http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html>> 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://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/> |
|||||||||||||||||||||||