phpjs
Version:
74 lines (61 loc) • 2.49 kB
Markdown
layout: page
title: "JavaScript bccomp function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/bccomp:877
- /functions/view/bccomp
- /functions/view/877
- /functions/bccomp:877
- /functions/877
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's bccomp
{% codeblock bc/bccomp.js lang:js https://raw.github.com/kvz/phpjs/master/functions/bc/bccomp.js raw on github %}
function bccomp (left_operand, right_operand, scale) {
// From: http://phpjs.org/functions
// + original by: lmeyrick (https://sourceforge.net/projects/bcmath-js/)
// - depends on: _phpjs_shared_bc
// * example 1: bccomp(1, 2);
// * returns 1: 3
// @todo: implement these testcases
// bcscale(0);
//
// bcmath.test.result('bccomp', 1, -1, bccomp('-1','5', 4));
// bcmath.test.result('bccomp', 2, -1, bccomp('1928372132132819737213', '8728932001983192837219398127471'));
// bcmath.test.result('bccomp', 3, 0, bccomp('1.00000000000000000001', '1', 2));
// bcmath.test.result('bccomp', 4, 1, bccomp('97321', '2321'));
var libbcmath = this._phpjs_shared_bc();
var first, second; //bc_num
if (typeof scale === 'undefined') {
scale = libbcmath.scale;
}
scale = ((scale < 0) ? 0 : scale);
first = libbcmath.bc_init_num();
second = libbcmath.bc_init_num();
first = libbcmath.bc_str2num(left_operand.toString(), scale); // note bc_ not php_str2num
second = libbcmath.bc_str2num(right_operand.toString(), scale); // note bc_ not php_str2num
return libbcmath.bc_compare(first, second, scale);
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/bc/bccomp.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/bc/bccomp.js)
### Example 1
This code
{% codeblock lang:js example %}
bccomp(1, 2);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
3
{% endcodeblock %}
### Other PHP functions in the bc extension
{% render_partial _includes/custom/bc.html %}