473,382 Members | 1,647 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,382 software developers and data experts.

not getting exact answer when adding floating numbers.

Ive got to sum up 5 numbers.
125000+23500.75+5525725.79+3257500+63542.36
When summing these i should get the answer as 8995268.9
but im getting the answer as 8995268.899999

My script code is as follows
------------------------------------------
var totalDebit =0;
(here dtAmt[i] contains each of the above specified numbers)
for(i=0;i<5;i++){
if(!isEmpty(dtAmt[i].value)){
totalDebit =parseFloat(totalDebit) +parseFloat(dtAmt[i].value);
}
}

Y do i get the anonymous answer.?
Feb 15 '07 #1
8 1452
r035198x
13,262 8TB
Ive got to sum up 5 numbers.
125000+23500.75+5525725.79+3257500+63542.36
When summing these i should get the answer as 8995268.9
but im getting the answer as 8995268.899999

My script code is as follows
------------------------------------------
var totalDebit =0;
(here dtAmt[i] contains each of the above specified numbers)
for(i=0;i<5;i++){
if(!isEmpty(dtAmt[i].value)){
totalDebit =parseFloat(totalDebit) +parseFloat(dtAmt[i].value);
}
}

Y do i get the anonymous answer.?
I will move this to the Javascript forum where it belongs and where you are likey to be told about how parseFloat approximates values ...
Feb 15 '07 #2
dorinbogdan
839 Expert 512MB
After calcualtion add the following formatting (to get 2 decimals):
Expand|Select|Wrap|Line Numbers
  1. totalDebit = totalDebit.toFixed(2);
  2.  
It seem that only works in IE 5.5+ and Netscape 6+.
Feb 15 '07 #3
dorinbogdan
839 Expert 512MB
Or, try this link for better approach and cross-browser support.
Feb 15 '07 #4
acoder
16,027 Expert Mod 8TB
Ive got to sum up 5 numbers.
125000+23500.75+5525725.79+3257500+63542.36
When summing these i should get the answer as 8995268.9
but im getting the answer as 8995268.899999

My script code is as follows
------------------------------------------
var totalDebit =0;
(here dtAmt[i] contains each of the above specified numbers)
for(i=0;i<5;i++){
if(!isEmpty(dtAmt[i].value)){
totalDebit =parseFloat(totalDebit) +parseFloat(dtAmt[i].value);
}
}

Y do i get the anonymous answer.?
Use
Expand|Select|Wrap|Line Numbers
  1. totalDebit.toFixed(1);
to round to one decimal place and yes, parseFloat is only approximate.
Feb 15 '07 #5
r035198x
13,262 8TB
Ah, so this was double posted!
Feb 15 '07 #6
acoder
16,027 Expert Mod 8TB
Threads merged.
Feb 16 '07 #7
when i add 1125345.55+105687.69+178954.89+86933.72

i should get the answer as 2172811.85 but i get the answer as 2172811.84999

the code is as
where dtAmt[i] consists of the numbers

var floatSum=0;
var intSum=0;
for(i=0;i<openingLedger1.length;i++){

if(!isEmpty(dtAmt[i].value)){
var amnt=dtAmt[i].value;
if(!isInteger(dtAmt[i].value)){
floatSum=parseFloat(floatSum)+parseFloat(amnt);
}
else{
intSum=parseFloat(intSum)+parseFloat(amnt);
}
totalDebit =parseFloat(floatSum) +parseFloat(intSum);
}
Mar 28 '07 #8
acoder
16,027 Expert Mod 8TB
See this page.
Mar 28 '07 #9

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

Similar topics

17
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
0
by: Bengt Richter | last post by:
I think for some purposes it might be handy to have and exact value representation in the compiler parse tree, without having to use strings as carrier. I.e., >>> from exactdec import ED >>>...
1
by: jel | last post by:
I ran what was submitted in several forums, but it's not exactly what i'm looking for. I'm dy'n over here. Ah, the frustrations of an amateur programmer. I included the code below in c++. which...
13
by: puzzlecracker | last post by:
If you run this program, it will give very unexpected results. Can anyone explain the nature of this anamaly? (also what is the function call to execute 'pause'); #include<stdio.h> //void...
6
by: David M. Synck | last post by:
Hi all, I am fairly new to Python and trying to figure out a syntax error concerning lists and iteration through the same. What I am trying to do is sum a list of float values and store the sum...
21
by: oksuresh | last post by:
Hi talents, I have noticed that atof() function approximates the string I pass to it. when I use atof() , as atof(" 184.64") and it returns 184.63999999999 But I would like to have...
34
by: linq936 | last post by:
Hi, I just did the following test: 1:void main(void){ 2: int p = 1; 3: int* pp = &p; 4: int c = p * pp; 5: int d = p + pp; 6:}
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...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.