-2

Is there a built in function in python tha can convert numbers like 5.76869 into binary?

I know how to do it by creating a function myself and bin only deals with integers

  • 2
    There are a few issues here - first, do you mean converting the exact decimal number 5.76869 to binary, or do you mean converting the float `5.76869` to binary? `5.76869` is not exactly 5.76869. Second, if you mean converting 5.76869 to binary, that number doesn't *have* a terminating binary representation. It's like converting 1/3 to decimal. How do you want to handle that? – user2357112 Dec 24 '22 at 11:05
  • 1
    Look here [Binary representation of float in Python (bits not hex)](https://stackoverflow.com/questions/16444726/binary-representation-of-float-in-python-bits-not-hex) – Muhammadyusuf Dec 24 '22 at 11:05
  • Unduping. That question is about getting the raw bits of the IEEE 754 binary64 format. This question is about base conversion. – user2357112 Dec 24 '22 at 11:07

1 Answers1

-2

In Python, we can simply use the bin () function to convert from a decimal value to its corresponding binary value. The bin () takes a value as its argument and returns a binary equivalent. Note bin () return binary value with the prefix 0b, so depending on the use-case, formatting should be done to remove 0b.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 4
    `bin` only handles integers. – user2357112 Dec 24 '22 at 11:05
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '22 at 14:39