469,637 Members | 1,564 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,637 developers. It's quick & easy.

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

Post your reply

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

Similar topics

17 posts views Thread by John Hunter | last post: by
13 posts views Thread by puzzlecracker | last post: by
6 posts views Thread by David M. Synck | last post: by
5 posts views Thread by =?Utf-8?B?anVzdGluc2FyYWNlbm8=?= | last post: by
135 posts views Thread by robinsiebler | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.