Use PHP functions in JavaScript

JavaScript bcmul

Returns the multiplication of two arbitrary precision numbers

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
function bcmul(left_operand, right_operand, scale) {
    // Returns the multiplication of two arbitrary precision numbers  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/bcmul    // +   original by: lmeyrick (https://sourceforge.net/projects/bcmath-js/)
    // -    depends on: _phpjs_shared_bc
    // *     example 1: bcmul(1, 2);
    // *     returns 1: 3
//  @todo: implement these testcases//        bcscale(0);
//
//        bcmath.test.result('bcmul', 1, '2', bcmul("1", "2"));
//        bcmath.test.result('bcmul', 2, '-15', bcmul("-3", "5"));
//        bcmath.test.result('bcmul', 3, '12193263111263526900', bcmul("1234567890", "9876543210"));//        bcmath.test.result('bcmul', 4, '3.75', bcmul("2.5", "1.5", 2));
//        bcmath.test.result('bcmul', 5, '13008.1522', bcmul("5573.33", "2.334", 4));
 
    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_multiply(first, second, scale);
 
    if (result.n_scale > scale) {
        result.n_scale = scale;
    }    return result.toString();
}
external links: original PHP docs | raw js source

Examples

Running

1
bcmul(1, 2);

Should return

1
3

Dependencies

In order to use this function, you also need:

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have bcmul goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

No comments yet. Be the first!


Contribute a New function

More functions

In this category

bcadd
bccomp
bcdiv
» bcmul
bcround
bcscale
bcsub

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !