Convert value to number, to integer, to decimal - Node.js Number

Node.js examples for Number:Decimal

Description

Convert value to number, to integer, to decimal

Demo Code

var NumberUtils = new function() {
    this.toNumber = function( val ) {
        if(val==null || val=="") return null;
    val = val.replace(',', '');
        var n = eval(val);
        return n;
    }/*from ww w  .ja  va2s.  c o  m*/

    this.toInteger = function( val ) {
    val = val.replace(',', '');
        return parseInt(val);
    }

    this.toDecimal = function( val ) {
    val = val.replace(',', '');
        if( val.indexOf( ".") < 0 ) {
            return eval(parseFloat(val).toFixed(2));
        }
        else {
            return eval(val);
        }
    }
}

Related Tutorials