Help with parseInt() issue

Help with parseInt() issue
0.0 0

#1

Something off is happening with parseInt where for some reason it sometimes rounds the number up from a decimal, and sometimes it doesn’t. I can’t seem to find the cause of this behavior. Any ideas?

var unitToggle = document.querySelector('#toggle');
var wUnit = document.querySelector('#unit');
unitToggle.addEventListener("click", toggleUnits);

function toggleUnits(){
  if(wUnit.innerHTML == "C"){
    var oldTemp = parseInt(wTemp.innerHTML, 10),
        newTemp = oldTemp * 9 / 5 + 32;
    wTemp.innerHTML= newTemp;
    wUnit.innerHTML = "F";
    unitToggle.innerHTML ="Switch to Celsius";
  }else{
    var oldTemp = parseInt(wTemp.innerHTML, 10),
        newTemp = (oldTemp-32) * 5 / 9;
    wTemp.innerHTML= newTemp;
    wUnit.innerHTML = "C";
    unitToggle.innerHTML ="Switch to Fahrenheit";
  }
}

CodePen: https://codepen.io/abenjamin/pen/ZojJLq


#2

You really only need to make one conversion (Celsius to Fahrenheit). When you need to display the temp in Celsius, you just use the original one you were given in the API response.

Not sure why you are trying to use parseInt to round the number in the span with id=“temp”. Just substitute Number for parseInt. You really do not want to round anything until the final calculation is made (newTemp). Then you could display it using toFixed.