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
Post a Comment