Gordon Burditt said:
If all you are transmitting is the binary representation of a
floating point number, there is no way to ensure that the final
string representation (if any) will only use 2 decimals. There are
only 4 patterns that have exact representations in binary floating
point that will naturally use only two or fewer decimal places:
xxxxx.00
xxxxx.25
xxxxx.50
xxxxx.75
and in any case, there's no way to tell that:
2.50000000000000000000000000000000000000000 as a binary floating point
number has one or 20 significant digits to the right of the decimal point,
as 2.5 and 2.500000000000 are represented the same. You need to transmit
the number of significant digits, or something equivalent, along with the
value.
If I want to transmit a number 2.5223987237476 that I know can only really
be the nearest of 2.40, 2.50 and 2.60, I would rather transmit it as
2.500000000000000072 or 2.499999999999999991 (by rounding to 1 decimal) than
as 2.5223987237476.
The tiny deviations from exactly 2.5 are inevitable. Any calculations using
the rounded number will be as accurate as they can be. And printed out in
any number of reasonable places will display as 2.5000000...
decimalround(x,0) can return an exact representation of the rounded
value within a reasonable range. decimalround(x,1) cannot.
There seems to be this preoccupation with this exact representation thing.
If you build a stack of 926 1cm cubes, the height should be 9.26m. That 9.26
of course cannot be represented exactly in binary, but is the best
representation possible.
In the same way, decimalround(9.2614779606,2) might not be exactly 9.26, but
is the best possible.