PHP's round in JavaScript

Here’s what our current JavaScript equivalent to PHP's round looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module.exports = function round (value, precision, mode) {
// discuss at: http://locutus.io/php/round/
// original by: Philip Peterson
// revised by: Onno Marsman (https://twitter.com/onnomarsman)
// revised by: T.Wild
// revised by: Rafał Kukawski (http://blog.kukawski.pl)
// input by: Greenseed
// input by: meo
// input by: William
// input by: Josep Sanz (http://www.ws3.es/)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// note 1: Great work. Ideas for improvement:
// note 1: - code more compliant with developer guidelines
// note 1: - for implementing PHP constant arguments look at
// note 1: the pathinfo() function, it offers the greatest
// note 1: flexibility & compatibility possible
// example 1: round(1241757, -3)
// returns 1: 1242000
// example 2: round(3.6)
// returns 2: 4
// example 3: round(2.835, 2)
// returns 3: 2.84
// example 4: round(1.1749999999999, 2)
// returns 4: 1.17
// example 5: round(58551.799999999996, 2)
// returns 5: 58551.8

var m, f, isHalf, sgn // helper variables
// making sure precision is integer
precision |= 0
m = Math.pow(10, precision)
value *= m
// sign of the number
sgn = (value > 0) | -(value < 0)
isHalf = value % 1 === 0.5 * sgn
f = Math.floor(value)

if (isHalf) {
switch (mode) {
case 'PHP_ROUND_HALF_DOWN':
// rounds .5 toward zero
value = f + (sgn < 0)
break
case 'PHP_ROUND_HALF_EVEN':
// rouds .5 towards the next even integer
value = f + (f % 2 * sgn)
break
case 'PHP_ROUND_HALF_ODD':
// rounds .5 towards the next odd integer
value = f + !(f % 2)
break
default:
// rounds .5 away from zero
value = f + (sgn > 0)
}
}

return (isHalf ? value : Math.round(value)) / m
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/math/round'). You could also require the math module in full so that you could access math.round instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • Great work. Ideas for improvement:

    • code more compliant with developer guidelines
    • for implementing PHP constant arguments look at the pathinfo() function, it offers the greatest flexibility & compatibility possible

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1round(1241757, -3)1242000
2round(3.6)4
3round(2.835, 2)2.84
4round(1.1749999999999, 2)1.17
5round(58551.799999999996, 2)58551.8

« More PHP math functions