Skip to content

Instantly share code, notes, and snippets.

@lancevo
Created July 16, 2013 16:10
Show Gist options
  • Save lancevo/6010145 to your computer and use it in GitHub Desktop.
Save lancevo/6010145 to your computer and use it in GitHub Desktop.
numberize() convert numbers in string format to float
// 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