phpjs
Version:
120 lines (104 loc) • 3.54 kB
Markdown
---
layout: page
title: "JavaScript round function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/round:505
- /functions/view/round
- /functions/view/505
- /functions/round:505
- /functions/505
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's round
{% codeblock math/round.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/round.js raw on github %}
function round (value, precision, mode) {
// From: http://phpjs.org/functions
// + original by: Philip Peterson
// + revised by: Onno Marsman
// + input by: Greenseed
// + revised by: T.Wild
// + input by: meo
// + input by: William
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Josep Sanz (http://www.ws3.es/)
// + revised by: Rafał Kukawski (http://blog.kukawski.pl/)
// % 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
precision |= 0; // making sure precision is integer
m = Math.pow(10, precision);
value *= m;
sgn = (value > 0) | -(value < 0); // sign of the number
isHalf = value % 1 === 0.5 * sgn;
f = Math.floor(value);
if (isHalf) {
switch (mode) {
case 'PHP_ROUND_HALF_DOWN':
value = f + (sgn < 0); // rounds .5 toward zero
break;
case 'PHP_ROUND_HALF_EVEN':
value = f + (f % 2 * sgn); // rouds .5 towards the next even integer
break;
case 'PHP_ROUND_HALF_ODD':
value = f + !(f % 2); // rounds .5 towards the next odd integer
break;
default:
value = f + (sgn > 0); // rounds .5 away from zero
}
}
return (isHalf ? value : Math.round(value)) / m;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/round.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/math/round.js)
### Example 1
This code
{% codeblock lang:js example %}
round(1241757, -3);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
1242000
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
round(3.6);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
4
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
round(2.835, 2);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
2.84
{% endcodeblock %}
### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}