Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pieter - 17 Jan 2006 10:18 GMT
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/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.