Parse price string to number - Node.js String

Node.js examples for String:Parse

Description

Parse price string to number

Demo Code

/**/*  w  w w. j  a  va  2 s.c  om*/
* 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