/    Sign up×
Community /Pin to ProfileBookmark

parseFloat Accuracy?

I’m wondering about the accuracy of parseFloat.
Why? Because I am trying to add dollars together taken from string and there are inconsistencies.

The following is an example adding 114.97 and 69.99:

<html>
<body>

<script type=”text/javascript”>

document.write(“parseFloat(‘114.97’)=”+parseFloat(‘114.97’)+”<br />”);
document.write(“parseFloat(‘69.99’)=”+parseFloat(‘69.99’)+”<br />”);
document.write(“parseFloat(‘114.97’) + parseFloat(69.99)=”+(parseFloat(‘114.97’)+parseFloat(‘69.99’))+”<br />”);

</script>

</body>
</html>

===============================

OUTPUT
===============================

parseFloat(‘114.97’)=114.97
parseFloat(‘69.99’)=69.99
parseFloat(‘114.97’) + parseFloat(69.99)=184.95999999999998

Can someone explain to me the above result?

Thanks…

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@scragarMar 31.2008 — parseFloat('114.97') + parseFloat(69.99)=184.95999999999998[/quote]
is all down to how decimals are calculated using binary addition(to repesent 0.5 in binary to the left of the decimal it uses 1, to repesent 0.25 it uses 01... unfortunatly, it just so happens that 0.7 and 0.3 don't have very good estimates: 01001101 = 0.300036...), try:
for(var i = 0; i &lt; 1; i+=0.1){document.write(i)}

it's nothing to do with parseFloat. your best bet it to use toFixed() to correct the accuracy to a feasable number:

document.write("parseFloat('114.97') + parseFloat(69.99)="+(parseFloat('114.97')+parseFloat('69.99')).toFixed(6)+"&lt;br /&gt;");
Copy linkTweet thisAlerts:
@Declan1991Mar 31.2008 — Didn't see the previous post. Already answered.
Copy linkTweet thisAlerts:
@akeaneauthorMar 31.2008 — Thanks... that is what I thought.

I did not know about the toFixed() method before I wrote something to convert to two decimal places and to display the value.

Okay... if this inconsistency is present and one is adding a number of values that all have been parseFloat'ed, is it not true that at some point the total will be really 'off the wall'?

I'm working on a reservation form with a calculator and well... I'd like to be able to tell them that I have fixed the problem and it should not accure again. I think I am safe since there are only three choices possible with three prices but I just thought I'd throw it out there.

Thanks....
Copy linkTweet thisAlerts:
@scragarMar 31.2008 — the difference is normaly only around 0.000,000,000,000,001 per number used at worse(remember, a lot of numbers have a few nice powers of 2 that add together to make it meaning that there is no loss), so as long as you use toFixed(10) from time to time it's fine.

if your completely unwilling to accept this there are string addition methods out there designed to tackle this problem, or the float problem that occurse rounding large numbers
Copy linkTweet thisAlerts:
@akeaneauthorMar 31.2008 — Thanks scrager ... I appreciate your quick response and confirmation of what I thought and the new method toFixed().
Copy linkTweet thisAlerts:
@felgallApr 01.2008 — If you are worried about ending up one cent out in the answer then simply multiply the amounts by 100 first and forget about decimal places diring the calculation, just divide by 100 again at the end.
Copy linkTweet thisAlerts:
@KorApr 01.2008 — Remember that the processor calculates in binary, so that the error will occur [I]only[/I] when you try display the result in decimal base. That means you don't need to fix that error during the calculations, because [I]there is no real error[/I] at the binary level of the processor. You may use toFixed() only at the end, to display the result. Note that toFixed() method transforms the number into a string.
×

Success!

Help @akeane 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 6.1,
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,
)...