#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    579
    Rep Power
    51

    Trouble with .toFixed() function


    I am pretty new to JavaScript and pretty much teaching myself and I learn best by trial and error...

    I have the following which uses the .toFixed() (function / attribute?). The second time I use it, (with 'unitSub' in the last line of the code below) it works as expected. However in the line before that (with 'unit_price') it fails and the JavaScript developer console says "Uncaught TypeError: unit_price.toFixed is not a function". If I remove the ".toFixed(2)", the value of 'unit_price' displays as expected but when I add it back it, it fails.

    As far as I can tell, the last two lines of the quoted code are the same, with the exception of the variable that's called. I am sure I am overlooking something simple... I just don't see it.

    Any guidance will be appreciated!

    Code:
    if(addm >0){
         var unit_price = document.getElementById('member_price['+j+']').value; 
    }else{
         var unit_price = document.getElementById('standard_price['+j+']').value; 
    }
    var unitSub = (unitCount * unit_price);
    grandTotal += unitSub;
    document.getElementById('unitPriceDisp'+j).innerHTML = '$'+unit_price.toFixed(2); 
    document.getElementById('unitSubDisp'+j).innerHTML = '$'+unitSub.toFixed(2);
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,903
    Rep Power
    9646
    If it has parentheses then it's a function.

    Javascript has different types, right? Like strings and numbers. Those correspond to actual classes, like String and Number. Even simple strings like "abc" or numbers like 123 are instances of those classes, which is why you can do stuff like
    Code:
    "abc".length // 3
    123.toFixed(2) // syntax error
    (123).toFixed(2) // 123.00
    .toFixed is a function (technically, method) on the Number class. The String class does not have one.
    The .value you got into unit_price is a string. Even if it's numeric, even if it came from a <input type=number>, it's still a string. So you can't .toFixed() it. But unitSub, which is the result of a calculation and therefore a number, can be .toFixed().

    If you want unit_price.toFixed(2) then you have to turn it into a number first. There are many, many ways you could, and one of them is
    Code:
    Number(unit_price).toFixed(2)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    579
    Rep Power
    51
    Perfect!


    Thanks so much...


    Originally Posted by requinix
    If it has parentheses then it's a function.

    Javascript has different types, right? Like strings and numbers. Those correspond to actual classes, like String and Number. Even simple strings like "abc" or numbers like 123 are instances of those classes, which is why you can do stuff like
    Code:
    "abc".length // 3
    123.toFixed(2) // syntax error
    (123).toFixed(2) // 123.00
    .toFixed is a function (technically, method) on the Number class. The String class does not have one.
    The .value you got into unit_price is a string. Even if it's numeric, even if it came from a <input type=number>, it's still a string. So you can't .toFixed() it. But unitSub, which is the result of a calculation and therefore a number, can be .toFixed().

    If you want unit_price.toFixed(2) then you have to turn it into a number first. There are many, many ways you could, and one of them is
    Code:
    Number(unit_price).toFixed(2)

IMN logo majestic logo threadwatch logo seochat tools logo