# gnc_numeric: fractional displays and rounding

Christian Stimming stimming at tuhh.de
Fri Jul 25 13:07:01 EDT 2008

```Am Freitag, 25. Juli 2008 20:15 schrieb Charles Day:
> So my first question: Is there an existing routine which will take a
> gnc_numeric and attempt to change its denominator to any exact power of
> ten?

No, there's not. Feel free to add one.

Also, the whole issue with rational numbers was probably an overkill solution
anyway. Of course binary floating point numbers (float/double) are the wrong
thing to do in financial calculations, but rational numbers are providing
more flexibility than needed. What should have been implemented at the time
are decimal (!) floating point numbers. I.e. instead of two large integers as
nominator and denominator (the current rational numbers), we should have used
one large integer for the mantissa m and a small integer e for the position
of the decimal point, so that the resulting number is m * 10^(e)  .  This is
different from "normal" floating point types which are always using a power
of two because of the machine's number representation in binary. But
whatever. Rational numbers is what we have right now, so that's what we will
stick with.

> The second thing I wanted to ask about is rounding. Currently many prices
> going into the price db experience rounding, and this causes bugs (such as
> bug 309863.)  Does anyone know WHY prices are being rounded? I suspect that
> this rounding is unnecessary and is being done just to avoid fractional
> display (see above).

No idea.

> If there were a routine that converted denominators to any power of ten,
> but only without rounding, then the price editor could use that on prices
> to make as many as possible stored in a way that prints in decimal form.

As prices from actual transactions involve the division of numbers, I'm afraid
the requirement "without rounding" might not be feasible in that case
anymore. There might be rounding with much more precision than what currently
happens, though, so I completely agree there is much room for improvement
here.

> Alternatively, the gnc_numeric printing routines such as xaccPrintAmount()
> use this conversion purely for printing purposes; this would avoid all
> unnecessary fractional display of gnc_numerics with only a small
> performance hit (I say "small" because these routines do most of the work
> already and just avoid the last few steps). Which way is preferred?

I think changing xaccPrintAmount() the way you described here is a good idea
in any case and should be implemented once you have that routine.

> Finally, I have already written such a routine. I hope that I have not
> reinvented the wheel...

Good to hear! Thanks for your work!

Christian
```