GobyDot
GobyDot

Reputation: 483

Rounding nearest to (1, 1.5, 2, 2.5 etc..) javascript

To clarify, the problem is how to round a number like this. i.e. 1.512 should round to 1.5 and 2.123 should round to 2 and 2.323 should round to 2.5 (javascript)

Upvotes: 3

Views: 1572

Answers (3)

thefourtheye
thefourtheye

Reputation: 239693

[1.512, 2.123, 2.323].forEach(function(currentNumber){
    console.log(Math.round(currentNumber * 2) / 2);
});

Output

1.5
2
2.5

Upvotes: 8

Kamlesh
Kamlesh

Reputation: 529

Use Math.round(x) function for rounding to nearest integer value

Upvotes: -1

Ringo
Ringo

Reputation: 3967

Try this:

 var num = 1.222;
 num = num.toFixed(1);

Upvotes: 0

Related Questions