0

I am trying to make a JS app that will convert Fahrenheit to Celsius/Kelvin. The user enters a number in a numerical textbox on the site and chooses what unit to convert to. In JS, the number is assigned to the variable 'a'

Here is my code:

//Celsius
document.getElementById("tmpVal").value = ((a-32)*(5/9) + "° C");
//Kelvin
document.getElementById("tmpVal").value = ((a + 459.67)*(5/9) + "° K");

For Celsius when I enter 39, I get the correct answer, 3.89. But for Kelvin instead of getting 277.039, I get 21922.0389. I'm assuming that it has to do with the variable type because it seems like it's adding 459.67 to the end of whatever is stored in a. Like 39 + 459.67 = 39459.67. I know adding stuff can get a little complicated but I don't know how to word my question in Google to find an answer.

Lucas
  • 48
  • 5
  • Can you add a working code snippet demonstrating the issue? – void Mar 03 '18 at 07:00
  • Most probably you variable a is string type that's why it is concatenating the other value, first convert a into the integer and then perform your arithmetic. – Hassan Imam Mar 03 '18 at 07:27

2 Answers2

1

Here is corrected way of calculation. Make calculation separately in some variable and then show desired result.

Here is working example :

function convertTmp() {
  var a = parseFloat(document.getElementById('entVal').value);
  var toC = (a-32)*(5/9);
  var toK = (a+459.67)*(5/9);
  var rn = parseInt(document.getElementById('lstRnd').selectedIndex);

  var result;
  if (document.getElementById('rbC').checked) {
   if (toC.toString() == 'NaN') {
     result = 'error';
    } else {
     result = toC.toFixed(rn) + '° C';
    }
  } else {
   if (toK.toString() == 'NaN') {
     result = 'error';
    } else {
     result = toK.toFixed(rn) + '° K';
    }
  }
  document.getElementById('tmpVal').innerHTML = result;
}
div {
 padding:10px;
 width:100px;
 border:solid 1px #800000;
 margin-top:20px;
}
<input type="text" value="" id="entVal" /><br><br>
Rounding decimals : 
<select id="lstRnd" onchange="convertTmp();">
  <option>0</option>
  <option>1</option>
  <option selected="selected">2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>
<br><br>
<input type="radio" name="rbCh" id="rbC" checked /><label for="rbC">° C</label>
<input type="radio" name="rbCh" id="rbK" /><label for="rbK">° K</label><br>
<input type="button" onclick="convertTmp();" value="Convert" />
<div id="tmpVal"></div>
nelek
  • 4,074
  • 3
  • 22
  • 35
  • A good answer should explain why the OP has their issue and how your code fixes it. Don't just post "corrected" code. – RobG Mar 03 '18 at 07:44
0

You have to convert string to integer

Ex.

//Celsius
document.getElementById("tmpVal").value = ((parseInt(a)-32)*(5/9) + "° C");

//Kelvin
document.getElementById("tmpVal").value = ((parseInt(a) + 459.67)*(5/9) + "°K");

Or you can use Number.parseInt(number in string format) instead.

By the way, I'm not sure what 'a' variable refer to but it looks like your equation is wrong too.

Semooze
  • 143
  • 10