Javascript always round up to X decimal places -


this question has answer here:

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

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -