Z
Z
zlFast2016-04-08 13:13:18
JavaScript
zlFast, 2016-04-08 13:13:18

How to round a number in javascript?

It is necessary that all non-integer numbers be rounded up to 0.5, that is, let's say I get the number 2, it remains two, if I get the number 2.1 or 2.7, then the number should be converted to 2.5, how to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sergey, 2016-04-08
@zlFast

it is possible like this:

function round(x) {
  var t = Math.floor(x);
  return (t === x) ? t : t + 0.5;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question