* Converts `value` to a number. * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to process. * @returns {number} Returns the number. * @example * * _.toNumber(3.2); * // => 3.2 * * _.toNumber(Number.MIN_VALUE); * // => 5e-324 * *
(value)
| 55198 | * // => 3.2 |
| 55199 | */ |
| 55200 | function toNumber(value) { |
| 55201 | if (typeof value == 'number') { |
| 55202 | return value; |
| 55203 | } |
| 55204 | if (isSymbol(value)) { |
| 55205 | return NAN; |
| 55206 | } |
| 55207 | if (isObject(value)) { |
| 55208 | var other = typeof value.valueOf == 'function' ? value.valueOf() : value; |
| 55209 | value = isObject(other) ? other + '' : other; |
| 55210 | } |
| 55211 | if (typeof value != 'string') { |
| 55212 | return value === 0 ? value : +value; |
| 55213 | } |
| 55214 | value = value.replace(reTrim, ''); |
| 55215 | var isBinary = reIsBinary.test(value); |
| 55216 | return isBinary || reIsOctal.test(value) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : reIsBadHex.test(value) ? NAN : +value; |
| 55217 | } |
| 55218 | |
| 55219 | module.exports = toNumber; |
| 55220 |