Use PHP functions in JavaScript

JavaScript bcround

!No description available for bcround. @php.js developers: Please update the function summary text file.

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
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
65
function bcround(val, precision) {
    // !No description available for bcround. @php.js developers: Please update the function summary text file.
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/bcround    // +   original by: lmeyrick (https://sourceforge.net/projects/bcmath-js/)
    // -    depends on: _phpjs_shared_bc
    // *     example 1: bcround(1, 2);
    // *     returns 1: 3
//  @todo: implement these testcases//        bcscale(0);
//
//        bcmath.test.result('bcround', 1, '-2', bcround('-1.5', 0));
//        bcmath.test.result('bcround', 2, '-1.1235', bcround('-1.1234567', 4));
//        bcmath.test.result('bcround', 3, '2', bcround('1.5', 0));//        bcmath.test.result('bcround', 4, '1.1235', bcround('1.1234567', 4));
//        bcmath.test.result('bcround', 5, '1', bcround('1.499999999', 0));
//        bcmath.test.result('bcround', 6, '2', bcround('1.5555555555555555555', 0));
//        bcmath.test.result('bcround', 7, '1.44', bcround('1.444999', 2));
//        bcmath.test.result('bcround', 8, '-1.44', bcround('-1.444999', 2)); 
    var libbcmath = this._phpjs_shared_bc();
 
    var temp, result, digit;
    var right_operand; 
    // create number
    temp  = libbcmath.bc_init_num();
    temp  = libbcmath.php_str2num(val.toString());
     // check if any rounding needs
    if (precision >= temp.n_scale) {
        // nothing to round, just add the zeros.
        while (temp.n_scale < precision) {
            temp.n_value[temp.n_len+temp.n_scale]=0;            temp.n_scale++;
        }
        return temp.toString();
    }
     // get the digit we are checking (1 after the precision)
    // loop through digits after the precision marker
    digit = temp.n_value[temp.n_len + precision];
 
    right_operand = libbcmath.bc_init_num();    right_operand = libbcmath.bc_new_num(1, precision);
 
    if (digit >= 5) {
        //round away from zero by adding 1 (or -1) at the "precision".. ie 1.44999 @ 3dp = (1.44999 + 0.001).toString().substr(0,5)
        right_operand.n_value[right_operand.n_len+right_operand.n_scale-1] = 1;        if (temp.n_sign == libbcmath.MINUS) {
            // round down
            right_operand.n_sign = libbcmath.MINUS;
        }
        result  = libbcmath.bc_add(temp, right_operand, precision);    } else {
        // leave-as-is.. just truncate it.
        result  = temp;
    }
     if (result.n_scale > precision) {
        result.n_scale = precision;
    }
    return result.toString();
}
external links: original PHP docs | raw js source

Examples

Running

1
bcround(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 bcround 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 !