3

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)

GobyDot
  • 483
  • 3
  • 15

3 Answers3

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

Output

1.5
2
2.5
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

Try this:

 var num = 1.222;
 num = num.toFixed(1);
Ringo
  • 3,795
  • 3
  • 22
  • 37
-1

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

Kamlesh
  • 511
  • 1
  • 7
  • 18