Decimal notation can have a fractional part (example: 12.25
).
Binary, octal and hexadecimal notations only allow to write integers (example: 0xff80
).
I suggest to allow binary, octal and hexadecimal notations to have a fractional part.
Example:
0b1100.01 // = 12.25
0o14.2 // = 12.25
0xC.4 = 12.25
I would like to point out that Number.prototype.toString(base)
already supports fractional numbers:
12.25.toString(2) // = '1100.01'
12.25.toString(8) // = '14.2'
12.25.toString(16) // = 'c.4'
12.25.toString(3) // = '110.0202020202020202020202020202021'
12.25.toString(36) // = 'c.9'
Number.parseFloat()
(and parseFloat()
) should also support base
parameter as Number.parseInt()
does:
Number.parseFloat('1100.01', 2) // = 12.25
Number.parseFloat('14.2', 8) // = 12.25
Number.parseFloat('C.4', 16) // = 12.25
Number.parseFloat('20.1', 3) // = 6.3333333333333333333
Number.parseFloat('12.45', 7) // = 9.673469387755102
Number.parseFloat('hello.world', 36) // = 29234652.90799883