Checks if {value} is between numbers {from} and {to} - Node.js Number

Node.js examples for Number:Compare

Description

Checks if {value} is between numbers {from} and {to}

Demo Code


/**//from  ww w . j  av  a2  s. c o  m
* Checks if {value} is between numbers {from} and {to}
* @param {string} value
* @param {string} from
* @param {string} to
* @returns {boolean}
*/
isBetween = function(value, from, to){
   return (from === null || from === '' || value >= utils.parseNumber(from)) &&
                   (to === null || to === '' || value <= utils.parseNumber(to));
}

/**
* Parse price string
* @param {string}
*/
parseNumber = function(value) {
            if (typeof value !== 'string') {
                return parseFloat(value);
            }
            var isDot = value.indexOf('.');
            var isComa = value.indexOf(',');
            if (isDot !== -1 && isComa !== -1) {
                if (isComa > isDot) {
                    value = value.replace('.', '').replace(',', '.');
                } else {
                    value = value.replace(',', '');
                }
            } else if (isComa !== -1) {
                value = value.replace(',', '.');
            }
            return parseFloat(value);
        }

Related Tutorials