Skip to content

Instantly share code, notes, and snippets.

@haydar-can
Forked from ronaldronson/parse-float.js
Created December 22, 2022 22:35
Show Gist options
  • Save haydar-can/a8a0a2ae250a97f53f89156cd32b4879 to your computer and use it in GitHub Desktop.
Save haydar-can/a8a0a2ae250a97f53f89156cd32b4879 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