473,804 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BUG? SqlDecimal = Single/Double: 4.475 becomes 4.4749999999999 996D ????

Hi,

I have a variable (m_sngPrixNetUn itaire) 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.PrixNetU nitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUn itaire
=4.474999999999 9996D...

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
Jan 17 '06 #1
4 1276
"Pieter" <pi**********@h otmail.com> wrote in message
news:uT******** ******@TK2MSFTN GP15.phx.gbl...
Hi,

I have a variable (m_sngPrixNetUn itaire) 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.PrixNetU nitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUn itaire
=4.474999999999 9996D...

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.
--
Jan 17 '06 #2
Ok! Thanks for the fast answer!

"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:fM******** ************@co mcast.com...
"Pieter" <pi**********@h otmail.com> wrote in message
news:uT******** ******@TK2MSFTN GP15.phx.gbl...
Hi,

I have a variable (m_sngPrixNetUn itaire) 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.PrixNetU nitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUn itaire
=4.474999999999 9996D...

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.
--

Jan 17 '06 #3
"Pieter" <pi**********@h otmail.com> schrieb:
I have a variable (m_sngPrixNetUn itaire) 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.PrixNetU nitaire), which is of datatype SqlDecimal.

But during the conversion the value changes, and MyDal.PrixNetUn itaire
=4.474999999999 9996D...


<URL:http://research.micros oft.com/~hollasch/cgindex/coding/ieeefloat.html>
<URL:http://www.math.grin.e du/~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/>

Jan 17 '06 #4
"Pieter" <pi**********@h otmail.com> schrieb
Hi,

I have a variable (m_sngPrixNetUn itaire) 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.PrixNetU nitaire), which is of datatype
SqlDecimal.

But during the conversion the value changes, and
MyDal.PrixNetUn itaire =4.474999999999 9996D...

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_sngPrixNetUni taire as Decimal or as SqlDecimal (it's Value
property is also Decimal).
Armin

Jan 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8264
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to understand.)
31
6655
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
0
3581
by: Daniel Zelisko | last post by:
Hello Could anyone tell me how to send a null decimal value to the sql server depending on its value? The following code gives me an error: System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values. I figured out SqlDecimal.Null is used only with db Money fields. But when I changed SqlDecimal.Null to DBNull.Value I got the error: CS0173 Type of conditional expression can't be...
2
1426
by: Mark | last post by:
Hi all, I have a DAL which takes in a value as shown below ready for my MSSQL stored procedure. protected SqlDecimal _annualLeave = SqlDecimal.Null; public SqlDecimal AnnualLeave { get {return _annualLeave;} set{ _annualLeave = value;}
7
21037
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem comes in when you copy a double quote from MS Word and paste it in the text box. What happens is the double quote becomes slanted (“) so my code above can't filter it. I tried to do this text= Replace(text, "““","'")
4
1626
by: Pieter | last post by:
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?
11
2260
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a Single with value 4.475 was converted to a Decimal with value 4.4749999999999996D. So after inserting and selecting it from the database I got another value than the original!
0
1886
by: Crazy Cat | last post by:
How in the hell does one specify the precision and scale for the SQLDecimal type? I am writing a user defined CLR SQL Server function and one of the function parameters is a decimal value which in SQL Server would be type numeric(18,6), but the only type that seems to be an equivalent on the VB 2005 side is SQLDecimal, but there seems no way to set the precision and scale for this (outside of using a new constructor, which is not allowed...
7
2713
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards Totto
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.