By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,570 Members | 982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,570 IT Pros & Developers. It's quick & easy.

not getting exact answer when adding floating numbers.

P: 58
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
Share this Question
Share on Google+
8 Replies

10K+
P: 13,262
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
Expert 100+
P: 839
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
Expert 100+
P: 839
Or, try this link for better approach and cross-browser support.
Feb 15 '07 #4

acoder
Expert Mod 15k+
P: 16,027
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

10K+
P: 13,262
Ah, so this was double posted!
Feb 15 '07 #6

acoder
Expert Mod 15k+
P: 16,027
Threads merged.
Feb 16 '07 #7

P: 58
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
Expert Mod 15k+
P: 16,027
See this page.
Mar 28 '07 #9

Post your reply

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