AntoineFr / flarum-ext-money

Give money to your users for different actions.
MIT License
22 stars 11 forks source link

Using the bcmath library instead of directly calculating on floating values #45

Open zxy19 opened 1 month ago

zxy19 commented 1 month ago

I noticed that the extension is using float type to calculate money. Float operations should be avoided in money calculation as it leads to unavoidable inaccuracy (such as 0.3000001). Could you use the bcmath library for high-precision calculations and change the type to currency or decimal in database?

AntoineFr commented 1 week ago

Hello @zxy19 , thank you for your suggestion.

For the decimal database type, I thought about it earlier but the problem is that you have to specify the number of decimals you want to keep, and this can vary a lot between 2 extension users. For the bcmath functions, they need a PHP module to be installed so it may not be portable, however I'll take a look at it.

To solve this problem, I also thought of storing the money as an integer in database, and add a display settings for this extension to divide the money visually in the frontend. For example, it would be stored in database as 1234567 with a division settings of 100 and it would display as 12345.67 in the forum.

zxy19 commented 1 week ago

Yes I think it's a good idea to use integer so that it would be more compatible with the large amount of extension which depends on this extension. Website administrator can easily make the old extension works by multiply the original money amount by 100 even the extension has not update.

I wonder if it will be accomplish in furture version?