0

lets say I have a number in the following form: 0.00N1N2N3...(for example 0.007).

I want to round the number 0.00N1N2N3...Nn, into the follwoing number: 0.0M1M2M3..Mn.

For example:0.007 need to be round to 0.01.

Now the number can be also in the following form 0.N1...Nn or N1.N2...Nn so the solution need to be generic for all cases. I have write the following function(Not sure if this is the right answer):

function roundup(number, precision) {
  return Math.ceil(number * precision) 
}
Brk
  • 1,247
  • 2
  • 23
  • 55

2 Answers2

2

If the variable is float you can use toFixed() like

var formatted = parseFloat("345.65894").toFixed(2);
geekbro
  • 1,293
  • 12
  • 13
1

On most browsers you can use the toFixed() function.

number.toFixed(precision)

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Christiaan Rakowski
  • 322
  • 2
  • 7
  • 15