Created
July 16, 2013 16:10
-
-
Save lancevo/6010145 to your computer and use it in GitHub Desktop.
numberize()
convert numbers in string format to float
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert number from string type to number type | |
// It's a shallow convertion only 1 level deep | |
// param {object|array|string|number} o variable to be converted | |
// return {object|array|string|number} converted variable | |
function numberize(o) { | |
if (typeof o === "string" || typeof o === "number") { | |
if (!isNaN(o)) { | |
o = parseFloat(o); | |
} | |
} else if (Object.prototype.toString.call(o) === '[object Array]') { | |
for (var i= 0, l = o.length; i<l; i++) { | |
if (!isNaN(o[i])) { | |
o[i] = parseFloat(o[i]); | |
} | |
} | |
} else if (typeof o === "object") { | |
for (var k in o) { | |
if (!isNaN(o[k])) { | |
o[k] = parseFloat(o[k]); | |
} | |
} | |
} | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment