Use PHP functions in JavaScript

JavaScript bccomp

Compares 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
function bccomp (left_operand, right_operand, scale) {
    // Compares two arbitrary precision numbers  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/bccomp    // +   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);}
external links: original PHP docs | raw js source

Examples

Running

1
bccomp(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 bccomp 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: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

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