Thứ Năm, 21 tháng 4, 2011

Các hàm javascript (is_float )


JavaScript is_float

Returns true if variable is float point
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
function is_float (mixed_var) {
    // Returns true if variable is float point  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/is_float    // +   original by: Paulo Freitas
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: WebDevHobo (http://webdevhobo.blogspot.com/)
    // %        note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
    // %        note 1: it different from the PHP implementation. We can't fix this unfortunately.    // *     example 1: is_float(186.31);
    // *     returns 1: true
    if (typeof mixed_var !== 'number') {
        return false;
    } 
    return !!(mixed_var % 1);
}
external links: original PHP docs | raw js source

Examples

Running
1
is_float(186.31);
Should return
1
true

Dependencies

No dependencies, you can use this function standalone.

Các hàm javascript (is_double )

function is_double (mixed_var) {
    // http://kevin.vanzonneveld.net
    // +   original by: Paulo Freitas
    //  -   depends on: is_float
    // %        note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
    // %        note 1: it different from the PHP implementation. We can't fix this unfortunately.
    // *     example 1: is_double(186.31);
    // *     returns 1: true
    return this.is_float(mixed_var);
}

Examples

Running
1
is_double(186.31);
Should return
1
true

Dependencies

In order to use this function, you also need: