Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2006, 04:35 PM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Strange calculation problem

I have a very strange problem. I am multiplying quantity by amount to get a sub total and for most quantities it works ok but for some reason when I enter 5 in the quantity I get a sub total with around 8 decimal places eg 2.99*5 gives me ($14.950000000000001), multiplied by4 gives me $11.96. This also happens on quantity 10,20,30 and so on.

Here is the function:

function totalord()
{
var totitems
var itm1 = document.form1.hambqty.value
var itm2 = document.form1.cheeseqty.value
var itm3 = document.form1.chickqty.value
var itm4 = document.form1.friesqty.value
var itm5 = document.form1.gravyq.value
var itm6 = document.form1.chilliq.value


totitems = eval(itm1+itm2+itm3+itm4+itm5+itm6)
alert("Item 1 is " + itm1)
if (totitems == "0")
    {
    alert("you have not orderd any items, please enter at least one item(s)")
    }


document.form1.hambsub.value = eval(document.form1.hambprice.value.substring(1,6) *itm1)


document.form1.hambsub.value =("$"+document.form1.hambsub.value)
alert("Subtotal 1 is " + document.form1.hambsub.value)

}

 
Old November 3rd, 2006, 11:33 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Just as in decimal system some numbers cannot be expressed perfectly, e.g. 1/3. The same happens in binary so you have to round numbers if it's important.
As a side issue, why are you using eval? It's completely unnecessary and a severe waste of resources.

--

Joe (Microsoft MVP - XML)
 
Old November 3rd, 2006, 01:13 PM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm new to javascript so you'll have to excuse me for not doing things the right way sometimes. I have removed the eval and used )itm1+"+"+itm2+"+"+itm3...etc, I was using eval because when adding the vars I was getting 122345 for an answer instead of the sum 17. Thanks for your input though, every little bit helps.

 
Old November 3rd, 2006, 01:43 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

There are a number of ways to do arithmetic summation, rather than string concatenation. You can use Math.floor:
Code:
var itm1 = document.form1.hambqty.value
var itm2 = document.form1.cheeseqty.value
alert(Math.floor(itm1) + Math.floor(itm2);
or the unary plus:
Code:
alert((+itm1) + (+itm2));
You will often see parseInt used as well but it is best avoided.

--

Joe (Microsoft MVP - XML)
 
Old November 3rd, 2006, 03:16 PM
Authorized User
 
Join Date: Nov 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've seen Math.floor used before but not with the alert, does that actually add them up or just display the sum. Thanks for the ideas, I'll try them out.






Similar Threads
Thread Thread Starter Forum Replies Last Post
strange IE problem sully7 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 September 19th, 2008 08:44 AM
Strange problem... F3553 C++ Programming 1 October 25th, 2007 03:56 PM
Strange ListBox problem mirandaym ASP.NET 1.0 and 1.1 Basics 3 August 2nd, 2006 08:30 AM
very strange problem! please help! raybristol ASP.NET 1.0 and 1.1 Basics 8 December 15th, 2005 06:46 AM
Strange runtime problem Cephus S SQL Server 2000 3 October 30th, 2004 10:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.