Use Decimal instead of Double for all financial calculations
Kentico ecommerce unfortunately suffers from rounding errors at times. The reason for this is because the Double type has been used rather than Decimal.
All financial calculations (e.g. calculating the total basket cost) should be done using the Decimal type - it's what it was created for because Double's simply don't offer enough precision.
Please stop using Double, and starting use Decimal instead.
Hello all, we are currently working on major changes in EC module. One of the goals is to use decimals in calculations. We are using decimal configuration 18.9. A major part of Kentico core already uses decimals. Feel free to leave your thoughts.
Sean Wright commented
Our 3rd party integration libraries for payment all use the decimal type, so we use decimal in our internal libraries and expose APIs to do the conversion from Kentico wherever our libraries touch Kentico because we know we will be given a double to deal with.
Seeing as how using a decimal for monetary calculations is a standard for c# community this would be a nice change and would not require a huge refactor (for our team at least).
Richard Shackleton commented
What is the status of this?
Hi Petr, I'm open for looking at this and I'm sure I can get some other developers over here to take a look.
Hi Petr - Unfortunately I think it would have to be a breaking change in the API. However I think it would be a worthwhile one, I'd have the confidence that all future projects are going to apply conversions correctly and charge a customer the right price.
Alternatively you could add a different property, on a ShoppingCart change TotalPrice to Price and mark the old one as deprecated
Honestly I'd say it was quite a big mistake not using Decimals in the first place. I think in this instance I would suggest making a breaking change (this would result in Double/Decimal) compile errors which are quite straight forward to fix to provide the benefit of a calculation system that actually works. I'd certainly take the hit spending an hour casting things back to Double if need be to have confidence and correctness in the underlying engine.
I honestly imagine people would be taking code out that works around these rounding errors so it would benefit most people.