Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Incorrect division

 perc = 15/30;
 //result=Math.round(perc*100)/100  //returns 28.45
 $('#counter').text(perc);
 $('#total').text(count);

returns back 0.5% which is suppose to be 50.00%... how do I fix this? :S

like image 415
test Avatar asked May 21 '26 14:05

test


2 Answers

You do realize that word percent quite literally translates into "per cent" or "per 100" since cent is the latin root that's used everywhere meaning "100" or "one-hundredth".

  • Century (100 years)
  • US Cent (100th of a dollar)
  • Centurion (Those who commanded 100 soldiers)
  • Centipede (creature with 100 legs)

So 50% becomes 50 per cent becomes 50 per 100

And, since in mathematical terms, the word per means divide (miles per hour == mph == m/h) then we can distill 50% down to:

50/100

Which, surprisingly enough, is represented as the decimal number .5

like image 147
Peter Bailey Avatar answered May 23 '26 23:05

Peter Bailey


15/30 = 0.5

if you want to have percent number you have to multiply it by 100.

like image 20
Tomasz Kowalczyk Avatar answered May 24 '26 00:05

Tomasz Kowalczyk