Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Codes fails if sum total is around .8 - why ?

1. ## Codes fails if sum total is around .8 - why ?

Hi all again

Can anyone tell me what I'm doing wrong as this code "seems" to work until the total reaches around .8

You can test this by adding 12.34 to the 1st, 2nd and 3rd boxes then you get an error - but if you change the last 12.34 to 12.35 it works fine.

The same happens if you add 50.7 to the 1st and then 50.1 to the 2nd - but it will work if the answer is not around .8

Thanks if you can spot my basic mistake - even better if you can suggest a better (simpler) method, as this is doing my head in :-)

Karen

Code:
```<html xmlns="http://www.w3.org/1999/xhtml">
<title></title>
<script type="text/javascript">
\$(function () {
var am_1 = \$('input:text[id\$=am_1]').keyup(foo);
var am_2 = \$('input:text[id\$=am_2]').keyup(foo);
var am_3 = \$('input:text[id\$=am_3]').keyup(foo);
var am_4 = \$('input:text[id\$=am_4]').keyup(foo);
var am_5 = \$('input:text[id\$=am_5]').keyup(foo);
var am_6 = \$('input:text[id\$=am_6]').keyup(foo);
var am_7 = \$('input:text[id\$=am_7]').keyup(foo);
var am_8 = \$('input:text[id\$=am_8]').keyup(foo);
function foo() {
var value1 = am_1.val();
var value2 = am_2.val();
var value3 = am_3.val();
var value4 = am_4.val();
var value5 = am_5.val();
var value6 = am_6.val();
var value7 = am_7.val();
var value8 = am_8.val();

var sum = add(value1, value2, value3, value4, value5, value6, value7, value8);
\$('input:text[id\$=TSum]').val(sum);
}

var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
if (IsNumeric(arguments[i])) {
sum += parseFloat(arguments[i]);
}
}
return sum;
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}
});
</script>
<body>
<form id="form1" runat="server">
<div>
<table width="300px" border="1" >
<tr><td><asp:TextBox runat="server" ID="am_1"></asp:TextBox></td></tr>
<tr><td><asp:TextBox runat="server" ID="am_2"></asp:TextBox></td></tr>
<tr><td><asp:TextBox runat="server" ID="am_3"></asp:TextBox></td></tr>
<tr><td><asp:TextBox runat="server" ID="am_4"></asp:TextBox></td></tr>
<tr><td><asp:TextBox runat="server" ID="am_5"></asp:TextBox></td></tr>
<tr><td><asp:TextBox runat="server" ID="am_6"></asp:TextBox></td></tr>
<tr><td><asp:TextBox runat="server" ID="am_7"></asp:TextBox></td></tr>
<tr><td><asp:TextBox runat="server" ID="am_8"></asp:TextBox></td></tr>

<tr><td>Total:&nbsp;&nbsp;<asp:TextBox runat="server" ID="TSum"></asp:TextBox></td>
</tr>
</table>
</div>
</form>
</body>
</html>```

• and it seems it's not just me.

http://www.texaswebdevelopers.com/ex...javascript.asp
This is the same problem (with a different code) from someone teaching people how to write code.

On thier website try adding 50.1 to the 1st box and 50.1 to the second - same problem.

I never was much use a math at school so I can't work this out

What am I missing please ??

• I take it you mean that 12.34 + 12.34 + 12.34 = 37.019999999999996 and 50.7 + 50.1 = 100.80000000000001

It happens because computers work in binary so they convert your number to the nearest binary equivalent first and then convert the binary back into the nearest decimal equivalent at the end.

ECMAScript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an accuracy of 15-16 decimal digits; integers up to just over 9e15 are precise, but few decimal fractions are. Given this, arithmetic is as exact as possible, but no more. Operations on integers are exact if the true result and all intermediates are integers within that range.

Other examples:-

Code:
```<script type = "text/javascript">

alert (5 * 1.015);  // does not give exactly 5.075

alert (0.06+0.01);  //  does not give exactly 0.07 in javascript.

alert (3355.53 + 660.97 - 660.97);

</script>```
If you need an exact answer to a set number of decimal places then shift the decimal point in all your numbers that many places to the right to make integers before doing the calculation and then shift it back after the calculation. For example with currencies that use two decimal places you should always multiply them all by 100 at the start and divide by 100 at the end or the answer might not be exact.

Code:
```<script type = "text/javascript">

var a = 50.7;
var b = 50.1;
var c = ((a*100)+(b*100))/100;

</script>```

“What is a cynic? A man who knows the price of everything and the value of nothing.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

• ## Users who have thanked Philip M for this post:

Reflo2 (03-09-2013)

• One addition: Be careful what kind of operations you're doing, though. With addition and subtraction it works fine, but multiplication, for example, will get you in trouble:

Code:
```var a = 4,
b = 5;

var aShifted = 100 * a,
bShifted = 100 * b;

console.log( a + b ); // 9
console.log( ( aShifted + bShifted ) / 100 ); // 9

// Multiplication not so much
console.log( a * b ); // 20
console.log( ( aShifted * bShifted ) / 100 ); // 2000```
Of course this doesn't come surprising if you know elementary school math, but there is a certain risk that one doesn't think about it. Not that the trick wouldn't work in this case, it just has to be slightly modified:

Code:
`console.log( ( aShifted * bShifted ) / 10000 ); // 20`
In other words: It's not as easy as simply dividing by what you multiplied the numbers with in the beginning. You'll actually have to think about what to divide by in the end.

• Hi Philip

i have tried to *100 then /100 within the code but it either does nothing only gives the answer 0.

eg
Code:
```                var sum = add(((value1*100), (value2*100), (value3*100), (value4*100), (value5*100), (value6*100), (value7*100), (value8*100))/100);
\$('input:text[id\$=TSum]').val(sum);```

Karen

• Hi Philip

i have tried to *100 then /100 within the code but it either does nothing only gives the answer 0.

eg
Code:
```                var sum = add(((value1*100), (value2*100), (value3*100), (value4*100), (value5*100), (value6*100), (value7*100), (value8*100))/100);
\$('input:text[id\$=TSum]').val(sum);```

Karen

ps - you are right in that users are adding currency (UK Pound) amounts

Code:
```function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
if (IsNumeric(arguments[i])) {
sum += 100 * parseFloat(arguments[i]);
}
}
return sum / 100;
}```

• ## Users who have thanked Airblader for this post:

Reflo2 (03-09-2013)

• Thank you Philip_M and Airbladder

Your help has solved a problem I have been working on for 2 days.

• Originally Posted by Reflo2
ROTFL How appropriate!

Reminds me of a message stuck on the front of one of those hot-air hand-drying machines in a university toilet

"To listen to a lecture by Professor X press here".

We are sorry that as a result of a typographical error we referred to the now deceased but well-known English actor Denis Prick.
This should of course have read Pe*is Price. - Daily Mirror

• Originally Posted by Philip M
How appropriate!
I did not criticize or attack you in this thread at all. If you want to make everything personal from now on because we have different views on some topics, be my guest. Let's start with the good laugh I get every time you complain about books and tutorials using "out-dated technology that became obsolete a long time ago", even though your code "style" became obsolete a decade ago and yet you go on teaching it to beginners. How's that for a hot air lecture? And how appropriate that your quote is about typography, which is partly about making what you do look nice and readable.

You'd think someone of your age is a little less of a child.

I did not criticize or attack you in this thread at all.

Would you be surprised to learn that nobody but you has ever raised that objection, in this forum or anywhere else? So what is it that you feel gives you (a newcomer to the forum) a licence to be so critical and combative?

I quite understand that you have no sense of humour, and feel unrestrained by any lack of self-esteem. But be aware that my motto is the same as that of the Kings of Scotland - Nemo me impune lacessit.

I see the purpose of this forum is to offer assistance to the OPs. Not to manufacture quarrels with other contributors.

• Originally Posted by Philip M
So what is it that you feel gives you (a newcomer to the forum) a licence to be so critical and combative?
All I did was criticize things that I find worth discussing. If you don't feel the same way, you are always welcome to not participate in such discussions. I don't see any rule or reason why I need to wait a few years before I'm allowed to have my opinion and share my thoughts on other people's posts. Just because you're older, just because you have been in this forum longer, your opinion is not "worth" more than mine (and vice versa).

I see the purpose of this forum is to offer assistance to the OPs. Not to manufacture quarrels with other contributors.
I saw that when I posted in this thread, adding a little warning to your post and helping the OP to rewrite his code, when you replied with a whole post dedicated to offending me.

All I did was criticize things that I find worth discussing. If you don't feel the same way, you are always welcome to not participate in such discussions. I don't see any rule or reason why I need to wait a few years before I'm allowed to have my opinion and share my thoughts on other people's posts. Just because you're older, just because you have been in this forum longer, your opinion is not "worth" more than mine (and vice versa).

Get wise to yourself. Most people would accept that in any subject the opinion of a Johnny-Come-Lately is not worth that of (far) older and more experienced people. You will recall that you received absolutely zero support from others in this forum.

What you are saying is that you are free to make posts which are dedicated to offending me, but start to scream when the position is reversed.

It was not me who corrupted your screen name to the comic Airbladder. All I did was laugh at something I found funny.
You would call that sharing my thoughts. And you frequently mis-spell my name.

But I have just realised that Airblade is the name of James Dyson's high-tech hand dryer which pumps out a blast of hot air. But you are quite free not to participate in any discussion about that!

• I am truly sorry if I have offended anyone by misspelling their name, that was not my intention.

I, like many people, having been browsing this forum for quite some time as it is an extremely valuable resource. It was only when I couldn’t find the answer I was looking for that I decided to join, so I could post a specific question.

I am not sure if there is a way that I can “redo” my thanks so that I can
alter the spelling. If there is a way can someone please tell me.

Once again I am very sorry and I honestly didn’t mean to offend anyone.

Karen

PS. He is a She :-)

• Originally Posted by Reflo2
I am truly sorry if I have offended anyone by misspelling their name, that was not my intention.

I, like many people, having been browsing this forum for quite some time as it is an extremely valuable resource. It was only when I couldn’t find the answer I was looking for that I decided to join, so I could post a specific question.

I am not sure if there is a way that I can “redo” my thanks so that I can
alter the spelling. If there is a way can someone please tell me.

Once again I am very sorry and I honestly didn’t mean to offend anyone.

Karen

PS. He is a She :-)

Karen - There is absolutely no need to apologise. Most people would just laugh at your innocent typo. Only the over-sensitive and over-serious would take offence. Airblader (who is devoid of a sense of humour) is cross with me (not you) for making fun of him.

•
Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•