/    Sign up×
Community /Pin to ProfileBookmark

Adding two numbers with decimals

Hi, looking for some help here. All I am trying to do is add two numbers. 138.9 + 27.33 and instead of 166.23 i get 166.2300000000002. Can anyone tell me why? Here is my code

function button1_onclick() {
var TallyAmt = 0;
ThisAmt = document.form1.text1.value;
TallyAmt = (TallyAmt + (ThisAmt * 1));
alert (TallyAmt);
ThisAmt = document.form1.text2.value;
TallyAmt = (TallyAmt + (ThisAmt *
1));
alert (TallyAmt);
ThisAmt = document.form1.text3.value;
TallyAmt = (TallyAmt + (ThisAmt * 1));
alert (TallyAmt);
}
Thanks a bunch

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@pyroJun 26.2003 — Use [URL=http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1200964]toFixed()[/URL] to return it to a certain number of decimal places.
Copy linkTweet thisAlerts:
@David_HarrisonJun 26.2003 — Computers are crap with decimals so I've found that this would be the best way to add two variables a and b:

var n=0;

var o=0;

while(Math.round(a*Math.pow(10,n))!=(a*Math.pow(10,n))){n++;}

while(Math.round(b*Math.pow(10,o))!=(b*Math.pow(10,o))){o++;}

var p=(n>=o)?n : o ; // Had to leave spaces here because : o turns into :o

a=a*Math.pow(10,p);

b=b*
Math.pow(10,p);

answer=(a+b)/Math.pow(10,p);

It's a bit long winded but it's more accurate.
Copy linkTweet thisAlerts:
@pyroJun 27.2003 — What's wrong with toFixed()?
Copy linkTweet thisAlerts:
@David_HarrisonJun 27.2003 — That will only work to return a specified amount of decimal places whereas mine will return the actual number of decimal numbers (up to about 20 anyway).
Copy linkTweet thisAlerts:
@pyroJun 27.2003 — 138.9 + 27.33 should only return two decimals.
Copy linkTweet thisAlerts:
@David_HarrisonJun 27.2003 — Yes, but if he used toFixed() and set it to return two decimal places, what if he wanted to add 23.8306 and 478.2669, they both have four decimal places.
Copy linkTweet thisAlerts:
@pyroJun 27.2003 — [i]Originally posted by lavalamp [/i]

[B]Yes, but if he used toFixed() and set it to return two decimal places, what if he wanted to add 23.8306 and 478.2669, they both have four decimal places. [/B][/QUOTE]
Umm... He would set it to return four decimal places...
Copy linkTweet thisAlerts:
@David_HarrisonJun 27.2003 — What if he didn't know how many dp's the numbers were going to be to?
×

Success!

Help @kevin86 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...