parsing - Javascript functions or methods that take radix? -
these functions take radix
argument:
num.tostring(radix);
parseint("123", radix);
is there other function or method takes radix
(number base), because want use parsefloat("1a.b4",radix)
.
but since don't imagine there lot of them, please name of them if know, useful, thanks.
edit: yes parsefloat("1a.b4",16)should 26.703125. if @ , num.tostring(radix) supports bases 2 36
in firefox console (333.444).tostring(36) equals "99.fzf9i56mi"
generalizing @paulpro's comment function, redefine parsefloat()
so:
parsefloat = function parsefloat (string, radix = 10) { if (radix == 10) { return this(string) } const [istring, fstring = '0'] = string.split('.') const inumber = parseint(istring, radix) const fnumber = parseint(fstring, radix) const flength = math.max(fnumber.tostring(radix).length, fstring.length) const sign = infinity / inumber === infinity ? 1 : -1 return inumber + sign * fnumber / radix ** flength }.bind(parsefloat) console.log(parsefloat('1a.b4', 16)) console.log(parsefloat(0.05.tostring(16), 16)) console.log(parsefloat('7', 16)) console.log(parsefloat('-0.8', 16))
i'm not sure if looking for, hope helps.
Comments
Post a Comment