return number as a float - Node.js Number

Node.js examples for Number:Float

Description

return number as a float

Demo Code

/**//w  ww .  j a  va 2 s .  c  o  m
 * capitalize - return this as a capitalized string.
 *
 * @return {String} - this value, capitalized.
 */
String.prototype.capitalize = function () {
    'use strict';
    return this.charAt(0).toUpperCase() + this.slice(1);
};

/**
 * toFloat - return this as a float
 *
 * @return {Float} - this value, as a float
 */
String.prototype.toFloat = function () {
    var val = this.replace(/[^\d\.-]/, '');
    val = parseFloat( val.match(/^-?\d+(\.\d+)?$/)[0] );
    if (typeof val !== 'number') {
        throw 'ERROR: "' + this + '" cannot be converted to a float value';
    }
    return val;
};

/**
 * Stringify the given value and test for a finite number value.
 *
 * @param value {Object} - candidate value to test
 * @return {Boolean}
 */
function isNumber(value) {
    return String(value).isNumber();
}

/**
 * Test a value to see if it is an array object.
 *
 * @param obj {Object} - candidate object to test
 * @return {Boolean}
 */
function isArray(obj) {
    return Object.prototype.toString.apply(obj) === '[object Array]';
}

Related Tutorials