JSL.number

From Projects Wiki

Jump to: navigation, search

This holds the number based operations. I am not sure why I have included this in the main code base - it don't have a lot of practical uses.

JLS.number Code

Contents

[edit] Arguments

number
The initial number - the original value

[edit] Methods

[edit] JSL.number.times( func )

Calls the function provided as the argument the specified number of times. It also gives the number of the current run as an argument to the user function.

[edit] Arguments

func
The Function

[edit] Example

JSL.number(5).times(function(i) { alert("Run #"+i); }); //Executes the function 5 times

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_number.js, Line 19
function(func) {
    var func = JSL._makeFunc(func,"i");
    for(var i=0; i<this.number; i++) {
        func.call(i);
    }
}

[edit] JSL.number.upto( upper_limit, func )

Calls the given function with the value of the current number as an argument.

[edit] Arguments

upper_limit
The upper limit of the loop - the function will be executed while initial_number
func
The Function

[edit] Example

//Calls the function 8 times - the alerts will be 
//  Run #2, Run #3, Run#4 ... Run #9, Run#10
JSL.number(2).upto(10, function(i) {alert("Run #"+i);});

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_number.js, Line 34
function(upper_limit, func) {
    var func = JSL._makeFunc(func,"i");
    for(var i=this.number; i<=upper_limit; i++) {
        func.call(i);
    }
}

[edit] JSL.number.round( decimal_points )

Function that could be used to round a number to a given decimal points. Returns the answer. Taken from http://www.openjs.com/scripts/maths/rounding_numbers.php

[edit] Arguments

decimal_points
The number of decimal points that should appear in the result

[edit] Example

JSL.number(23.2833).round(2); //returns 23.28

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_number.js, Line 47
function(decimal_points) {
    if(!decimal_points) return Math.round(this.number);
    if(this.number == 0) {
        var decimals = "";
        for(var i=0;i<decimal_points;i++) decimals += "0";
        return "0."+decimals;
    }
 
    var exponent = Math.pow(10, decimal_points);
    var num = Math.round((this.number * exponent)).toString();
    return Number(num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points));
}
Personal tools