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
  • Select an option

  • Save haydar-can/a8a0a2ae250a97f53f89156cd32b4879 to your computer and use it in GitHub Desktop.

Select an option

Save haydar-can/a8a0a2ae250a97f53f89156cd32b4879 to your computer and use it in GitHub Desktop.

Revisions

  1. @ronaldronson ronaldronson created this gist Jan 13, 2014.
    34 changes: 34 additions & 0 deletions parse-float.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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;
    }