简体   繁体   中英

toString(16) with a decimal point

I understand it for binary, but I'm not comprehending it for hex.

Here's some examples:

Number(0.15).toString(16) // 0.26666666666666
Number(1.5).toString(16) // 1.8
Number(.18).toString(16) // 0.2e147ae147ae14

It looks like the decimal is increased by about 40% when converted to hex, which doesn't really make sense when you're going from base 10 to base 16. It should go down as it would with whole numbers, right?

If it is/isn't correct: how do you do this by hand? I fully understand converting integers but I haven't found a source on converting dec to hex when the dec value has a decimal.

First, don't think of the decimal as "increasing by about 40%." The numbers are equal - they're just represented using different number systems.

Let's take a look at what's going on when you convert a decimal number to binary, then to hex. For me, at least, this makes it a little easier to understand:

Take the decimal number 0.15 : In binary, it's 0.0010 0110 0110 0110 0110 0110 0110 0110... . (Spaces added for clarity.) Notice anything interesting? If you convert each of these chunks of four bits to the hex equivalents, you get exactly what your toString(16) evocation gave you: 0.2666666... ( 0010 in binary = 2 in hex).

Why is it this way? Because after the decimal point, you can think of each entry as an inverse power of 2 . Meaning each bit after the decimal point represents:

 .1 |  .01 |  .001 |  .0001 |  .00001... (BIN)
0.5 | 0.25 | 0.125 | 0.0625 | 0.03125... (DEC)

So how would we represent 1.5 in binary ? Well, everything before the decimal should be easy (it's just 1 ), but after that, we simply stop at 1.1 (binary), since 1 + 0.5 (decimal) = 1.5 (decimal) = 1.1000 (binary).

I threw some extra zeros in there to make this next conversion easier, because next we take that chunk of four bits and convert it to hex, which is simply 1.8 .

Interesting question that I have never thought about before, but after a little research, makes complete sense.

If you wanted to represent the decimal value 24 in hex, you would divide the value by 16 like this: 24/16 = 1, remainder 8 ---> so the "16's" digit in hex is 1 and the "1's" digit is 8, giving you the final hex value of 18 .

The "fractional" part of a decimal number goes through a sort of similar process, but, instead of dividing by 16, you multiply by 16. So, using your examples, you can step through the process:

Decimal number 0.15 = Hexidecimal number 0.26666666666666

  • .15 * 16 = 2.4 ---> hex value 0.2 (keep the 2, carry over .4)
  • .4 * 16 = 6.4 ---> hex value 0.26 (keep the 6, carry over .4)
  • .4 * 16 = 6.4 ---> hex value 0.266 (keep the 6, carry over .4)
  • .4 * 16 = 6.4 ---> hex value 0.2666 (keep the 6, carry over .4)
  • . . . .

Decimal number 1.5 = Hexidecimal number 1.8

  • 1.5 ---> hex value 1 (keep the 1, carry over .5)
  • .5 * 16 = 8 ---> hex value 1.8

Decimal number .18 = Hexidecimal number 0.2e147ae147ae14

  • .18 * 16 = 2.88 ---> hex value 0.2 (keep the 2, carry over .88)
  • .88 * 16 = 14.08 ---> hex value 0.2e (keep the 14 [or "e"], carry over .8)
  • .08 * 16 = 1.28 ---> hex value 0.2e1 (keep the 1, carry over .28)
  • .28 * 16 = 4.48 ---> hex value 0.2e14 (keep the 4, carry over .48)
  • . . . .

It actually makes a lot of sense once you step through it a bit, but it's certainly not something that presents itself clearly at first glance. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM