473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Trouble adding two double values

Hai ,

This is a bit weird for me.It may be that i got it wrong, but i request you to respond please.

All i need to do is add two double values

Dim a as double =324.0
Dim b as double=0.1
Dim c ad double

c=a+b


i expected the output to be c= 324.1 but i am getting 324.1015625, i tried using decimal datatype also but same problem.

i am inserting c value in sql server 2005 db there i am getting c value as 324.1015625, but in the code i am getting 324.1, why is this?,the datatype field for c in db is float.


please give me the vb.net code handle this.
Jun 12 '07 #1
5 1907
chinu
36
Hai ,

This is a bit weird for me.It may be that i got it wrong, but i request you to respond please.

All i need to do is add two double values

Dim a as double =324.0
Dim b as double=0.1
Dim c ad double

c=a+b


i expected the output to be c= 324.1 but i am getting 324.1015625, i tried using decimal datatype also but same problem.

i am inserting c value in sql server 2005 db there i am getting c value as 324.1015625, but in the code i am getting 324.1, why is this?,the datatype field for c in db is float.


please give me the vb.net code handle this.
Its quite suprising to see this as i dont think anyone will get other than 324.1 as I also tried in both C# and VB.net. can u delete the pdb files and open the project once again and try.
Jun 12 '07 #2
Motoma
3,237 Expert 2GB
I believe this is due to decimal rounding on the server.

When I do the following:
Expand|Select|Wrap|Line Numbers
  1. DECLARE @a AS FLOAT
  2. SET @a = 324.1
  3. SELECT @a
  4.  
I recieve 324.100000000002 as my result.

If I were to venture a guess, it would go something like this: SQL Server is going to try and store the data in such a way that the value you INSERT and the value you SELECT will be the same value. There are tricks and hacks in the driver to get this to happen, and this is what you are seeing here.
Jun 12 '07 #3
i still have the same problem please help , i tried using decimal datatype in .net but i am getting the same result
Jun 14 '07 #4
Motoma
3,237 Expert 2GB
i still have the same problem please help , i tried using decimal datatype in .net but i am getting the same result
What amount of precision are you aiming for? Can't you use one of the rounding functions to get what you are looking for?
Jun 14 '07 #5
hi
i am having the same problem, but a work around this is

double a =2.1
double b = 0.1
double c = System.Convert.ToDouble(( a+b).ToString());

i know this is not a good solution but will do tell i can find something else, please update as well if you have a better solution

thanks
Jun 22 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Anders Würtz | last post by:
i have an assignment to iterate through a collection containing different types of numeric values (float, double, int, byte, short etc.) and to add 1 to all of them. I tried with array and...
2
by: bellerophon | last post by:
I am having a serious trouble with my pointers in a programm that uses dll and was hoping that somebody in this group could help me: I export my DLL classes with declspec(dllexport) and link...
5
by: John Doe | last post by:
I have a not-so-large double in the range 1-1000, but with fractional part. I only do ++my_double to increment it, however sometimes it happens that after incrementing int(old_double) ==...
5
by: =?Utf-8?B?anVzdGluc2FyYWNlbm8=?= | last post by:
I'm using .net 2.0 and am having a problem adding doubles: double x = 63881.97 + 34297.98; The result of this addition in Visual Studio is 98179.950000000012 This is obviously the incorrect...
6
by: Peted | last post by:
Hi, im wanting to store some custom text strings in the app.config file of a c# app, to be retreived and updated when the app runs. using c# 2005 express in my testing i am using the code...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
10
by: Guillermo_Lopez | last post by:
Hello All, I am using VBA in access to perform some calculations. There is a particular sumation that is wrong (barely). this code is withing a loop. TDist = TDist + TempDist Both TDist...
2
by: Canye | last post by:
I have a very long list of codes that i was advised to put in a module but don't know where to start from. Also i have a botton in my software that i what to use to be adding tabs when running my...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.