views:

63

answers:

2

Hello, Can anyone please tell me how can I convert this float number: 12,25 to binary? I know how to convert the "12" but not the 0.25

Any help is much appreciated. Thanks

+1  A: 

Keep Multiplying the number after decimal by 2 till it becomes 1.0

eg. 0.25*2 = 0.50

0.50*2 = 1.00

and the result is in reverse order being .01

Abhi
I got it, That seems logic now, Thank you :)
Slim Black
@Slim Black: Beware: that works fine for numbers like 0.25, which have exact representations in binary, but not for numbers like 0.1, which don't: 0.1*2 = 0.2, 0.2*2 = 0.4, 0.4*2 = 0.8, 0.8*2 = 1.6, 0.6*2 = 1.2, 0.2*2 = 0.4, ... It repeats forever, and the result is 0.0(0011) (the part in parentheses repeats).
Rick Regan
@Slim Black: And note, to implement this correctly programmatically, you'll need decimal arithmetic -- see my article http://www.exploringbinary.com/base-conversion-in-php-using-bcmath/, specifically section dec2bin_f() .)
Rick Regan
Ok, thank you I'll check this :)
Slim Black
A: 

(d means decimal, b means binary)

1.) You write 12d in binary and remove it from your float. Only the remainder (.25d) will be left. 2.) You write the dot. 3.) while the remainder (0.25d) is not zero, multiply it with 2 (-> 0.50d), remove and write the digit left of the dot (0), and continue with the new remainder (.50d).

comonad
Yes, Thank you for you answer :)
Slim Black