# 32nd format (thirty seconds)

In working with trading systems, I came across an unusual format for representing numbers representing securities quotes, in particular for US government bonds. For example, the price presented `100-31`does not mean 100 dollars and 31 cents, or `100-127`does not make much sense at all, since there is only 100 cents in one dollar, not 1000, and there is no need to reserve three decimal places for the fractional part.

The trick here is that this is not a familiar decimal notation. For example, `100-31`in decimal it is equal to `100.97265625`, but `100-127`matches `100.40234375`.

So, this format for writing fractional numbers is called "thirty seconds" or `32nd`. For visual convenience and a clear difference from the decimal form, a small dash is used as a separator instead of a dot. But the number itself in general has the following format:

`AAA.XXY`

where `AAA`is the integer part of the number that has the same meaning as in the decimal system. `XX`- this is the number of `1/32`shares from the fractional part, and `Y`is the number of eights ( `1/8`) in the last `1/32`fraction. Despite the vague description, the transfer formula for the number `AAA.XXY`format `32nd`to decimal format is quite simple:

`D = AAA + (XX + Y * 1/8) * 1/32`

or

`D = AAA + XX * (1/32) + Y * (1/256)`

that is, for the number `100-127`AAA = 100, XX = 12, Y = 7, therefore:

`D = 100 + 12/32 + 7/256 = 100.40234375`

In order for the formula to be correct, it `XX`can take values ​​only from `"00"`to `"31"`, and `Y`from `"0"`to `"7"`. Also, when writing, the `Y`number `4`can be replaced by `+`, and `"0"`by a space. That is, `100-31`in full form, it is equal `100-310`, but `100-12+`equivalent `100-124`.

It can be seen that in three fractional digits, not 1000 parts are encoded, as in the decimal system, but only 256 (32 * 8).

So again: if it is written `100-12+`, then it is `100.39062500`in decimal system.

The reverse translation formula from decimal to format is `32nd`not much more complicated. Let be`D`decimal::

`A = TRUNC(D)`
`XX = TRUNC((D - A) * 32)`
`Y = ((D - A) * 32 - XX) * 8`

`TRUNC`is the function of taking the integer part.

If `Y`equal to 0, then you can not write this digit, and if 4, then you can replace it with `+`.

The component `Y`must be integer. Otherwise, the presence of the fractional part of y `Y`is a sign that the original decimal number `D`does not have a format mapping `32nd`(only 256 values ​​of the fractional part out of all 1000 possible can have a correspondence in the format `32nd`).

No matter how bizarre this method of recording cash amounts looks, it is traders who use it (not to be confused with raiders), for example, when trading in US government bonds. I can assume that this is just a legacy of the times when not everyone knew fractional decimal numbers, and writing parts of the whole in the form of natural fractions is much closer to human nature. Divide the pile into two, three, etc. parts can even be a child untrained in decimal fractions.

The format is strange, but you have to know it.