EN
JavaScript - Math.round() method example
8 points
Math
round
is a static method that returns a number that is rounded to the closest integer number.
xxxxxxxxxx
1
console.log( Math.round( 5 ) ); // 5
2
3
console.log( Math.round( 2.49 ) ); // 2
4
console.log( Math.round( 2.50 ) ); // 3
5
console.log( Math.round( 2.51 ) ); // 3
6
7
console.log( Math.round( -2.49 ) ); // -2
8
console.log( Math.round( -2.50 ) ); // -2
9
console.log( Math.round( -2.51 ) ); // -3
10
11
console.log( Math.round( 0.999 ) ); // 1
12
console.log( Math.round( 1.001 ) ); // 1
13
console.log( Math.round( -1.001 ) ); // -1
Syntax | Math.round(number) |
Parameters | number - integer or float number value (primitive value). |
Result | Rounded number value (primitive value). |
Description | round is a static method that returns a number that is rounded to the closest integer number. |
This section contains a custom function that shows how to round numbers with precision to n
places.
xxxxxxxxxx
1
function roundPrecised(number, precision) {
2
var power = Math.pow(10, precision);
3
return Math.round(number * power) / power;
4
}
5
6
7
// Usage example:
8
9
console.log( roundPrecised( 5 , 0 ) ); // 5
10
console.log( roundPrecised( 5. , 0 ) ); // 5
11
console.log( roundPrecised( .5, 0 ) ); // 1
12
13
console.log( roundPrecised( 1.2345, 0 ) ); // 1
14
console.log( roundPrecised( 1.2345, 1 ) ); // 1.2
15
console.log( roundPrecised( 1.2345, 2 ) ); // 1.23
16
console.log( roundPrecised( 1.2345, 3 ) ); // 1.235
17
18
console.log( roundPrecised( -1.2345, 0 ) ); // -1
19
console.log( roundPrecised( -1.2345, 1 ) ); // -1.2
20
console.log( roundPrecised( -1.2345, 2 ) ); // -1.23
21
console.log( roundPrecised( -1.2345, 3 ) ); // -1.234
22
23
console.log( roundPrecised( 12345, -1 ) ); // 12350
24
console.log( roundPrecised( 12345, -2 ) ); // 12300
25
console.log( roundPrecised( 12345, -3 ) ); // 12000
This section contains custom round function implementation.
xxxxxxxxxx
1
function roundNumber(value) {
2
if (value < 0.0) {
3
var rest = (value % 1.0);
4
if(rest < -0.5) {
5
rest += 1.0;
6
}
7
return value - rest;
8
} else {
9
value += 0.5;
10
return value - (value % 1.0);
11
}
12
}
13
14
15
// Usage example:
16
17
console.log( roundNumber( 5 ) ); // 5
18
19
console.log( roundNumber( 2.49 ) ); // 2
20
console.log( roundNumber( 2.50 ) ); // 3
21
console.log( roundNumber( 2.51 ) ); // 3
22
23
console.log( roundNumber( 0.999 ) ); // 1
24
console.log( roundNumber( 1.001 ) ); // 1
25
26
console.log( roundNumber( -2.49 ) ); // -2
27
console.log( roundNumber( -2.50 ) ); // -2
28
console.log( roundNumber( -2.51 ) ); // -3
29
30
console.log( roundNumber( -1.001 ) ); // -1
A simple plot with canvas is presented for Math.round
function below.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style> #canvas { border: 1px solid black; } </style>
5
</head>
6
<body>
7
<canvas id="canvas" width="200" height="200"></canvas>
8
<script>
9
10
var canvas = document.querySelector('#canvas');
11
var context = canvas.getContext('2d');
12
13
// cosine chart range
14
var x1 = -10.0;
15
var x2 = +10.0;
16
var y1 = -10.0;
17
var y2 = +10.0;
18
19
var dx = 0.01;
20
21
var xRange = x2 - x1;
22
var yRange = y2 - y1;
23
24
function calculatePoint(x) {
25
var y = Math.round(x);
26
27
// chart will be reversed horizontally because of reversed canvas pixels
28
29
var nx = (x - x1) / xRange; // normalized x
30
var ny = 1.0 - (y - y1) / yRange; // normalized y
31
32
var point = {
33
x: nx * canvas.width,
34
y: ny * canvas.height
35
};
36
37
return point;
38
}
39
40
console.log('x range: <' + x1 + '; ' + x2 + '>');
41
console.log('y range: <' + y1 + '; ' + y2 + '>');
42
43
var point = calculatePoint(x1);
44
45
context.beginPath();
46
context.moveTo(point.x, point.y);
47
48
for (var x = x1 + dx; x < x2; x += dx) {
49
point = calculatePoint(x);
50
context.lineTo(point.x, point.y);
51
}
52
53
point = calculatePoint(x2);
54
context.lineTo(point.x, point.y);
55
context.stroke();
56
57
</script>
58
</body>
59
</html>