webD97/rewe-ebon-parser

Floating point error in missing item check

Closed this issue · 3 comments

Thank you for building this library. I'm using it to automatically parse incoming mails containing eBons into a database, so that we can easily distribute costs via Splitwise.

This anonymized eBon 2020-03-13T19_17_00.000Z.pdf produces the following error:
Error: Something went wrong when parsing the eBon: The eBon states a total sum of 1964.9999999999998 but the parser only found items worth 1965.

My quick workaround was to round the totals to two after-comma digits before comparing them:

if (realTotalInCents.toFixed(2) != totalInCents.toFixed(2)) {

Hi @bitowl,
thanks for your bug report! Unfortunately I wasn't able to reproduce the behavior locally but I added your fix anyway. Can you please verify if it works now with latest master? If it does, I will release a new build.

Btw, cool use case :)

Here is a reproduction: https://repl.it/repls/VapidNeighboringUnderstanding
And it is working for this PDF with the latest master: https://repl.it/repls/AshamedAcademicPasswords

I'm not sure if it works in all cases, but as long as there are no items with fractions of a cent, this should work.

Thanks for your help! I have just released 0.3.1 containing this fix.

I also cannot say if it will always work but if it fixes at least some crashes it's worth it.