/    Sign up×
Community /Pin to ProfileBookmark

Trouble with parsing text to float…

I’m trying to parse some text inputs into floats and then add them together, but it isn’t working for some reason….

35.87 and 35.86 are the inputs however when added together they give 71.72999999999999 which makes no sense, is there some weird rounding thing I’m not understanding? But there wouldn’t even be a need for rounding, they add up perfectly to 71.73.

bk13amt=parseFloat(bk13amt.value);
bk7amt=parseFloat(bk7amt.value);
currentnum = bk13amt + bk7amt;

Anyone have any ideas?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzSep 19.2007 — The rounding error is caused by converting between decimal and binary.

[url=http://forums.devshed.com/showpost.php?p=1498326&postcount=3]Kor explains a bit more here.[/url]
Copy linkTweet thisAlerts:
@xvszeroauthorSep 19.2007 — Yeah tofixed seems to work, but I'm still a bit nervous.
Copy linkTweet thisAlerts:
@KravvitzSep 19.2007 — Nervous? Why?

//This snippet adds the method in older browsers that don't support it natively:
if(!0.1.toFixed && Number.prototype){
// for IE versions older than 5.5 and Netscape 4.x.
Number.prototype.toFixed=function(d){
d=(d&&d>1?((d<=20)?Math.floor(d):20):0);
var n=this,f=Math.pow(10,d);
n=((Math.round(n*f)/f)+Math.pow(10,-(d+1)))+'';
return n.substring(0,n.indexOf('.')+(d==0?0:d+1));
}
}
×

Success!

Help @xvszero 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...