scurker / currency.js

A javascript library for handling currencies

Home Page:https://currency.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extra Cent on Tax Inclusive Calculation / Banker's Rounding

fahimalizain opened this issue · comments

Please consider an Item that costs $40 with inclusive tax of 5%. When we break this into SubTotal, TaxAmount and GrandTotal, the following is what we get:

- SubTotal = 40 - (40 * 5 / (100 + 5)) = 38.0952.. = $38.10
- Tax Amount = 38.1 * 5 / 100 = 1.905 = $1.91
- Grand Total = 38.10 + 1.91 = $40.01                                    <-- Extra 0.01 Cent

The expectation is that Bankers Rounding / Round to Nearest, Ties to Even will fix this issue?

Use higher precision when doing the multiplication / division.

The following should give you the same grand total. But yeah, the package is missing the Bankers Rounding. However, tax is only rounded when filing the tax report. $1.91 is what you see on the tax invoice.

const total = currency(40, {precision: 4});
const subtotal = total.divide(1.05)
const tax = subtotal.multiply(0.05)

console.log(subtotal.value, ' -> ', currency(subtotal, { precision: 2 }).value)
console.log(tax.value, ' -> ', currency(tax, subtotal).value)
console.log(subtotal.add(tax).value)```