Skip to content

Instantly share code, notes, and snippets.

@ronaldronson
Created January 13, 2014 09:26
Show Gist options
  • Save ronaldronson/8397087 to your computer and use it in GitHub Desktop.
Save ronaldronson/8397087 to your computer and use it in GitHub Desktop.
Own parseFloat function
var parseFloat = function (val){
"use strict";
var res = NaN, i = 0, len, neg = false;
if ("number" == typeof val) {
return val;
}
if (null == val
|| "object" typeof val
|| "function" typeof val
|| !val.length
) {
return res;
}
if (!!~["+","-"].indexOf(val[0])) {
neg = "-" == val[0];
val = val.slice(1);
}
len = val.length;
if (!len) {
return res;
}
for (res = 0; i < len; i++) {
if (val[i] > 9 )
res = res * 10 + val[1];
}
return neg ? 0 - res : res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment