html - JavaScript code returning to many digits after decimal -


i have code need number arrives return 1 or 2 digits after decimal not 15 now, here have.

function getdiff (dt) {      smins = " min";     shours = " hrs";     sdays = " days";      if ( math.abs (datediff ("n", now, dt)) < 1440 ) {         if ( math.abs (datediff ("n", now, dt)) <= 60 ) {             return (math.abs (datediff ("n", now, dt)) + smins);         }         else         {             return (math.abs (datediff ("n", now, dt)/60) + shours);         }     }     else     {         return (math.abs (datediff ("n", now, dt)/1440) + sdays);     } } 

you can use .tofixed(2) format number 2 decimal places.

note .tofixed() returns string if want work result number again, you'll need parsefloat().

function getdiff (dt) {      smins = " min";     shours = " hrs";     sdays = " days";      if ( math.abs (datediff ("n", now, dt)) < 1440 ) {         if ( math.abs (datediff ("n", now, dt)) <= 60 ) {             return (math.abs (datediff ("n", now, dt)) + smins).tofixed(2);         }         else         {             return (math.abs (datediff ("n", now, dt)/60) + shours).tofixed(2);         }     }     else     {         return (math.abs (datediff ("n", now, dt)/1440) + sdays).tofixed(2);     } } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -