phpjs
Version:
91 lines (76 loc) • 3.07 kB
Markdown
---
layout: page
title: "JavaScript bcdiv function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/bcdiv:871
- /functions/view/bcdiv
- /functions/view/871
- /functions/bcdiv:871
- /functions/871
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's bcdiv
{% codeblock bc/bcdiv.js lang:js https://raw.github.com/kvz/phpjs/master/functions/bc/bcdiv.js raw on github %}
function bcdiv (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: bcdiv(1, 2);
// * returns 1: 3
// @todo: implement these testcases
// bcscale(0);
//
// bcmath.test.result('bcdiv', 1, '0', bcdiv("1", "2"));
// bcmath.test.result('bcdiv', 2, '0.50', bcdiv("1", "2", 2));
// bcmath.test.result('bcdiv', 3, '-0.2000', bcdiv("-1", "5", 4));
// bcmath.test.result('bcdiv', 4, '3333.3333', bcdiv("10000.0000", "3", 4));
// bcmath.test.result('bcdiv', 5, '2387.8877', bcdiv("5573.33", "2.334", 4));
// bcmath.test.result('bcdiv', 7, '1.00', bcdiv('6.00', '6.00', 2));
// bcmath.test.result('bcdiv', 8, '1.00', bcdiv('2.00', '2.00', 2));
// bcmath.test.result('bcdiv', 9, '59.51111111', bcdiv('66.95', '1.125', 8));
// bcmath.test.result('bcdiv', 10, '4526580661.75', bcdiv('8728932001983192837219398127471.00', '1928372132132819737213.00', 2));
var libbcmath = this._phpjs_shared_bc();
var first, second, result;
if (typeof scale === 'undefined') {
scale = libbcmath.scale;
}
scale = ((scale < 0) ? 0 : scale);
// create objects
first = libbcmath.bc_init_num();
second = libbcmath.bc_init_num();
result = libbcmath.bc_init_num();
first = libbcmath.php_str2num(left_operand.toString());
second = libbcmath.php_str2num(right_operand.toString());
result = libbcmath.bc_divide(first, second, scale);
if (result === -1) {
// error
throw new Error(11, "(BC) Division by zero");
}
if (result.n_scale > scale) {
result.n_scale = scale;
}
return result.toString();
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/bc/bcdiv.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/bcdiv.js)
### Example 1
This code
{% codeblock lang:js example %}
bcdiv(1, 2);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
3
{% endcodeblock %}
### Other PHP functions in the bc extension
{% render_partial _includes/custom/bc.html %}