allow binary, octal and hexadecimal notation to have a fractional part

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
2 Likes

One way to achieve this now is to include one extra character: +.

0b111+.5 // 7.5

Indeed, but in this case, the fractional part is expressed in decimal notation.

1 Like

What's the use-case for this? Binary and hex notation are useful for representing integers and bit patterns, but that doesn't require any fractional portion. Almost nothing written in binary or hex is non-integer values, in fact.

Do you have something specific that you're doing that requires (a) entering numeric literals with a fractional component, that are (b) best expressed as hex or binary? Or is this just a "this would be neat" kind of feature request?