Conversation
There was a problem hiding this comment.
- Please add a test case for this in
packages/data-format/test/rnf_invoice/utils.test.ts. Do you know where this issue occurs exactly? I'm not sure this is the way to go at it, to me it looks like it's more of a display issue rather than a calculation issue.
Edit: see comment below
|
@leoslr I understand now why this issue occurs, just saw your message on Slack. I think it might be a mistake to represent fiat currencies with only 2 decimals. But without changing the way every other components work, you might want to take a look at the FixedNumber class. It does exactly what we need as it has a const fixedNumber = FixedNumber.from(item.unitPrice)
// account for floating quantities
.mulUnsafe(FixedNumber.from(item.quantity * preciselyOne))
.divUnsafe(FixedNumber.from(preciselyOne))
.subUnsafe(FixedNumber.from(discount))
// Artificially offset the decimal to let the multiplication work
.mulUnsafe(FixedNumber.from(taxPercent * preciselyOne))
// Remove the decimal offset
.divUnsafe(FixedNumber.from(preciselyOne))
// Remove the percentage multiplier
.divUnsafe(FixedNumber.from(100))
.addUnsafe(FixedNumber.from(taxFixed))
.round(0);
console.log(fixedNumber.toString());Just tested real quick but i'm sure you can optimize more, like remove the |
|
@alexandre-abrioux Nice solution with the FixedNumber class, indeed I also added the test describing the edge case and it passes. (I checked again but indeed, it was not passing before). |
Co-authored-by: Alexandre ABRIOUX <alexandre-abrioux@users.noreply.github.com>
Fixes edge cases where we lost precision.
e.g. :
item.unitPrice = 33.33item.quantity = 1item.tax.amount = 20item.discount = 0The "true" result is
39.996. This function returned39.99, it now returns40.00.