toFixed() doesn't seem to work... why?

Posted on
  • Life happened so I put my project to the side for a couple of months. I decided to pick the project back up today and updated the firmware on the Espruino. By the way I really like the new GUI of the IDE :-)

    The issue I am having is toFixed(2) doesn't seem to get the decimal place to only two decimal places. toFixed(2) seemed to work before I took a break from my project.

    I hit the Send to Espruino button. I get the following output.

    _____ _ | |_ ___ ___ _ ||___ ___ | |_
    -| . | _| | | | | . | |
    || || |_|||_|_|

          |_| http://espruino.com  1v64 Copyright 2014 G.Williams
    

    echo(0);
    80.6646153846153879385383334010839462280­2734375 0
    161.403076923076923776534385979175567626­953125 1
    241.993846153846163815615000203251838684­08203125 2
    322.732307692307699653611052781343460083­0078125 3
    403.544615384615383391064824536442756652­83203125 4
    484.209230769230771329603157937526702880­859375 5
    564.833629007477156847016885876655578613­28125 6
    645.498244392092601628974080085754394531­25 7
    726.089013622861784824635833501815795898­4375 8
    806.622811603695026860805228352546691894­53125 9 Temp: 80.6622811603695026860805228352546691894­53125
    =undefined

    CODE:

    function onInit() {
    
      function getResTempValue(x) {
        var temp=0;
        var avgTemp=0;
        for (var i=0;i<x;i++)
        {
          var vOut = E.getAnalogVRef() * analogRead(A0); // if you attached VOUT to Ao
          var vZero = 0.4;
          var tCoeff =  19.5 / 1000;
          var tempinc = (vOut - vZero) / tCoeff;
          var tempinf = tempinc * (9 / 5) + 32;
          temp = temp + tempinf;
          console.log(temp);
          console.log(i);
        }
        temp = temp / x;
        temp.toFixed(2);  //<-- Doesn't seem to work.
    
        console.log("Temp: " + temp);
        return temp;
      }
    
      getResTempValue(10);
    }
    
    onInit();
    
  • Number.toFixed() returns a string with the specified format.

    What you would want to do is:

    var tfixed = temp.toFixed(2);  
        console.log("Temp: " + tfixed);
        return temp; //we probably want to return the number, not a string
    

    or

        console.log("Temp: " + temp.toFixed(2));
        return temp;
    
  • Thanks @DrAzzy

    As far as I'm aware that's never worked, so I'm not quite sure why everything was fine for you before!

  • DrAzzy, thanks for your help. Gordon, your right the code never worked. Which explains why I was getting erratic values from my sensor readings. Now, my sensor readings are stable.

  • Code that works:

    function onInit() {
      var cmd="";
      Serial1.setup(9600/*baud*/);
      Serial1.onData(function (e) {
        console.log("RX Data: " + e.data);
      });
    
      function getResTempValue(x) {
        var temp=0;
        var avgTemp=0;
        var tfixed=0;
    
        for (var i=0;i<x;i++)
        {
          var vOut = E.getAnalogVRef() * analogRead(A0); // if you attached VOUT to Ao
          var vZero = 0.4;
          var tCoeff =  19.5 / 1000;
          var tempinc = (vOut - vZero) / tCoeff;
          var tempinf = tempinc * (9 / 5) + 32;
          temp = temp + tempinf;
          console.log(temp);
          console.log(i);
        }
        temp = temp / x;
        tfixed = temp.toFixed(2);
    
        console.log("Temp: " + tfixed);
        return temp;
      }
    
      getResTempValue(10);
    }
    
    onInit();
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

toFixed() doesn't seem to work... why?

Posted by Avatar for d0773d @d0773d

Actions