Javascript always round up to X decimal places -
this question has answer here:
- round @ 2 decimal places (only if necessary) 40 answers
- how round number in javascript? 8 answers
i need write function round up, either round 0, 1 or 2 decimal places, depending on if function passed 0, 1 or 2.
examples...
round 0 decimal places: 13.4178 = 14
round 1 decimal place: 13.4178 = 13.5
round 2 decimal places: 13.4178 = 13.42
i've found math.ceil rounds whole integer , to fixed() round or down, not up. there way round in way i've described above?
you use factor multiplication ten , power of wanted decimal count , round , adjust dot.
function up(v, n) { return math.ceil(v * math.pow(10, n)) / math.pow(10, n); } console.log(up(13.4178, 0)); console.log(up(13.4178, 1)); console.log(up(13.4178, 2));
Comments
Post a Comment