Currency Conversion for E-Commerce Site - Preventing incorrect full basket due to rounding

I am adding multi-currency support to an e-commerce application. The way I approached the problem was to save the base currency in it and make the template call the priceDisplay () / plugin function anytime it displays the price. Thus, the template continues to receive prices in dollars. The priceDisplay function correctly converts the price, if necessary, and adds the correct $ or Euro sign depending on the settings of the viewers stored in the session. When ordering an application, the application will store the order in a dollar amount, as well as a currency code and currency. In addition, we will charge the customer’s credit card in our currency to ensure that they receive an invoice, exactly what was shown on the order screen.

Now the problem is to display the totals of the basket in the basket, as well as during the check. For example, the application sends prices to the template, which will be displayed in the basket:

subtotal: 9.75
ship: 5.95
total: 15.70

The template accepts these amounts and calls the priceDisplay function for each item. If the exchange rate is 1.1, then we will receive a message to the user:

subtotal: 10.725 → 10.73
ship: 6.545 → 6.55
total: 17.27

You can see that subtotal + ship = 17.28, but the overall conversion is 17.27.

So, a few options that, I think, can work, although not fully thought out:

  • Handle all conversions on the application side
  • , priceDisplay, , . , , 15.70, , , 15.71 15.69 ( exchangeRate .)
  • / - "" . , , 10.725, 5 . , 6.545, .005, . , , 2 ?
  • .

, PHP, - Smarty.

:
3 x 9,75 = 29,25
:
3 10,73 (10,725) = 32,18 (32,175)
3 x 10,73 = 32,19!= 32,18

+3
4

1 . - -, , .

, , . . , , gumballs 10 , , 100 10 gumballs. , , 100 .

. Martin Fowler " ". . :

, . , , , , .

+5

, .

+1

, , . , , , .

0

Do not use floating point numbers for money, ever! You allow yourself into the world of pain with rounding for no good reason. Since PHP does not have a decimal type with a fixed point, do all the money calculations using integers. See William References.

0
source

Source: https://habr.com/ru/post/1712888/


All Articles