SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Strange problem

  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strange 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)

    }

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try replacing
    Code:
    document.form1.hambsub.value = eval(document.form1.hambprice.value.substring(1,6)*itm1)
    with
    Code:
    document.form1.hambsub.value = (parseFloat(document.form1.hambprice.value.substring(1,6)) * itm1).toFixed(2);
    The key is the toFixed method of the Number object, read about it here
    http://msdn.microsoft.com/library/en...asp?frame=true

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This worked very well, thanks very much,. I tried this earlier and was on the right trail I just had my syntax wrong.


Bookmarks

Posting Permissions

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