It is important to use well-defined rounding rules for a specific application. If the billing application indicates that all position amounts will be rounded to the nearest penny, then the total invoice amount should reflect this. If he indicates that all calculations will be carried out to hundredths of a penny, then the invoice should probably reflect this (perhaps formatting fractional pennies in the form of small superscripts, as gas prices do). In some cases, fractional pennies could be distributed among the elements of the account on an ongoing basis, for example.
old_total = total
old_rounded_total = rounded_total
total = total + line_item
rounded_total = round_to_penny (total)
displayed_line_item_cost = rounded_total - old_rounded_total
In other cases, you can go around all lines of accounts to the nearest penny, subtract from this amount the sum of all rounded lines of accounts and adjust up or down those lines that are closest to the "penny" border.
If rounding semantics are well defined and the application follows them, the results will exactly match the specification. If semantics are not defined, the results will also not be.
source share