black_belt
black_belt

Reputation: 6799

Facing Problem with a Math Function

 var num1 = "5.54";
 var num2 = "10";
 result = (num1/num2)*100;

With javascript the result shows 50. But If I do the math using a calculator I find 55.4 which is correct. Would you please kindly help with the javascript code to make the result 55.4.

...

Hi,I have managed to make it work using parseFloat example- var num1 = parseFloat(document.getElementById("obtainedmark").value); var num2 = parseFloat(document.getElementById("totalmark").value);

Thanks.:)

Upvotes: 0

Views: 107

Answers (3)

user893096
user893096

Reputation: 443

I wrote a java script function

  function disp_text1()
   {
      var num1 = "5.54";
                var num2 = "10";
                result = (num1/num2)*100 ;
                alert(result);

   }

i got 55.400000000000006....on calling the function...try it again....

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

It works for me.

Upvotes: 2

Headshota
Headshota

Reputation: 21449

use parseFloat:

result = (parseFloat(num1)/parseFloat(num2))*100 ;

edit:

I just checked and it works without parsing as well. The problem is somewhere else in your code.

Upvotes: 1

Related Questions