-
-
Save haydar-can/a8a0a2ae250a97f53f89156cd32b4879 to your computer and use it in GitHub Desktop.
Own parseFloat function
This file contains 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
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