/    Sign up×
Community /Pin to ProfileBookmark

Rounding problem with parseFloat

I am having trouble when doing simple addition on strings I convert with parseFloat(). If I take 125.99 + 0.99 I get 126.97. This does not always behave this way. If I take 125.99 + 0.98 I get 126.97. The numbers are in a group of text fields, with the same name, on an HTML form. As I go through this input field array I add each number to the current accumulated total. Any direction would be appreciated.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Mar 28.2005 — <HTML>

<Head>

<Script Language=JavaScript>

function calcSum(){

isSum = 0;
nVals = document.getElementsByName('toSum');
for (i=0; i<nVals.length; i++)
{isSum += parseFloat(nVals[i].value)}
isSum = isSum.toFixed(2);
document.forms.Form1.nSum.value = isSum;
}


</Script>

</Head>

<Body>

<Form name='Form1'>

<input type=text value='126.99' name='toSum' size='7'><br>

<input type=text value='.99' name='toSum' size='7'><br>

<input type=text value='125.98' name='toSum' size='7'><br>

<input type=text value='1.90' name='toSum' size='7'><br>

<input type=text value='.98' name='toSum' size='7'><br><br>

Sum: <input type=text name='nSum' size='7'>

</Form>

<input type=button value='Test' onclick="calcSum()">

</Body>

</HTML>
Copy linkTweet thisAlerts:
@js_prof_consMar 28.2005 — I am having trouble when doing simple addition on strings I convert with parseFloat(). If I take 125.99 + 0.99 I get 126.97. This does not always behave this way. If I take 125.99 + 0.98 I get 126.97. The numbers are in a group of text fields, with the same name, on an HTML form. As I go through this input field array I add each number to the current accumulated total. Any direction would be appreciated.[/QUOTE]This has to do with the fact that reals are inaccurate. That's why large scale corporations use Integers to do everything. For example, your problem:

125.99 * 100 =&gt; 12599
0.99 * 100 =&gt; [u] + 99[/u]
12698 / 100 =&gt; 126.98
Copy linkTweet thisAlerts:
@xmike1authorMar 28.2005 — Thanks folks. Both of these work just fine. Much appreciated.
Copy linkTweet thisAlerts:
@js_prof_consMar 28.2005 — Thanks folks. Both of these work just fine. Much appreciated.[/QUOTE]I forgot to give an example of such a corpoartion. One off the top of my head is NASA.
Copy linkTweet thisAlerts:
@blkfdMay 16.2005 — I am not a java programer but tend to cut and paste java when I need to. I've been looking for simple code to sum two number fields which exactly what you've posted here. It does just what I need except I need to capture the number fields so they need to have unique names like (toSum1 + toSum2). Can I do that by modifying this code?

Thanks,

Joe
×

Success!

Help @xmike1 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.16,
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,
)...