# Print a rational number in binary format

**URL:** https://discuss.ocaml.org/t/print-a-rational-number-in-binary-format/13643
**Category:** Community
**Tags:** stdlib, printf, binary
**Created:** [December 15, 2023, 7:18am UTC](https://discuss.ocaml.org/t/print-a-rational-number-in-binary-format/13643 "2023-12-15T07:18:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![UnixJunkie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/unixjunkie/32/638_2.png) [@UnixJunkie](https://discuss.ocaml.org/u/UnixJunkie)
#### Post date: [December 15, 2023, 7:18am UTC](https://discuss.ocaml.org/t/print-a-rational-number-in-binary-format/13643/1 "2023-12-15T07:18:11Z")

</div>

I have a number x such that 0 \<= x \< 1.0.  
I would like to print x in binary format.

e.g. x=1/1024 prints “0.0000000001”  
x=1/2 prints “0.1”

Am I right that the stdlib’s Printf “only” supports octal, decimal and hexadecimal?

I guess I am good for coding the conversion then.

---

<div class="post-metadata">

### Author: ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)
#### Post date: [December 15, 2023, 8:55am UTC](https://discuss.ocaml.org/t/print-a-rational-number-in-binary-format/13643/2 "2023-12-15T08:55:07Z")

</div>

You can get the binary representation of a float using `Int64.bits_of_float` which will give you the bit pattern you are looking for except for the scaling.

---

<div class="post-metadata">

### Author: ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)
#### Post date: [December 15, 2023, 9:15am UTC](https://discuss.ocaml.org/t/print-a-rational-number-in-binary-format/13643/3 "2023-12-15T09:15:45Z")

</div>

> [@UnixJunkie](#):
>
> Am I right that the stdlib’s Printf “only” supports octal, decimal and hexadecimal?

As far as I know, yes.  
Although it looks like the `%b` specifier for booleans is deprecated (with `%B` as the recommended alternative), possibly with the aim to reuse it for integers in binary format in a future release.
