javascript - Round to nearest X -
there simple answer question can't seem find it. have number (dollar amount) such 0.34, round nearest x, meaning if x 0.15 want round 0.15, 0.3, 0.45, 0.6, 0.75 or 0.9. example don't want exact numbers, want able make x between 0.01 , 0.5 , number can rounded increment.
this question here rounds nearest 0.25 kind of want buy want generic function rounds numbers decimal portion set fraction.
number.prototype.round2dec = function(x){ return /* "this" round nearest x */; };
number.prototype.round2dec = function(x){ return (math.round(this * (1/x)) / (1/x)); };
from example given, 4 = 1/(0.25)
, expanded pattern x.
edit - looking through answers, realized have answer there, not expanding prototype
- round nearest .25 javascript.
Comments
Post a Comment