in reply to Re: (Ovid) Re: Unwanted implicit conversion to float
in thread Unwanted implicit conversion to float
The cleanest way is of course to keep $val as is and to round it only when printing it.
Perhaps I misunderstand what you mean by "clean", but the safest way to deal with money is to round the internal number any time you print something out1, and use that rounded number for all future calculations. If you are doing calculations with only whole-cent amounts (such as adding up a bunch of deposits), then you should round after every operation (or at least frequently) to prevent tiny round-off errors from accumulating such that your final total is off by one cent when it is finally rounded.
If you are doing calculations with fractional cents, then you need to define exactly where the rounding occurs (for example, interest is applied one a month and is always rounded at that time).
1 Update: The reason I say "every time you print something out" is that you (almost always) need to round if the value is being reported to the user. You can (and often should) do internal, intermediate calculations in fractional cents, but whenever you report [or could report] an amount to the user, you need to round your internal representation of that amount the same way you [would] round the amount you [could] report.
- tye (but my friends call me "Tye")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re2: Unwanted implicit conversion to float
by mirod (Canon) on Mar 12, 2001 at 23:54 UTC |