电子商务网站的货币转换-防止由于舍入而导致的总购物车错误 [英] Currency Conversion for E-commerce site - Preventing incorrect Total Cart due to rounding

查看:74
本文介绍了电子商务网站的货币转换-防止由于舍入而导致的总购物车错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为电子商务应用程序添加多币种支持。我解决问题的方法是使应用程序保持本位币不变,并在模板显示价格时让其调用priceDisplay()函数/插件。因此,模板继续以美元金额接收价格。如果需要,priceDisplay函数可以正确转换价格,并根据会话中存储的查看器设置添加正确的美元或欧元符号。在提交订单时,应用程序将以美元金额以及currencyCode和currencyRate存储订单。另外,我们将以客户的币种收取客户的信用卡费用,以确保他们准确地按订单屏幕上显示的价格计费。

I'm adding multi-currency support to an e-commerce application. The way I approached the problem was to keep the application in it's base currency and have the template call a priceDisplay() function/plugin anytime it displays a price. So the template continues to receive prices in dollar amounts. The priceDisplay function correctly converts the price if needed and adds the correct $ or Euro sign depending on the viewers settings, stored in the session. On order submit, the application will store the order in dollar amount as well as the currencyCode and currencyRate. Additionally we will charge the customer's credit card in their currency to ensure they get billed exactly what was shown on the order screen.

现在,在显示购物车中以及结帐时的购物车总数。例如,应用程序向模板发送要在购物车中显示的价格:

Now the issue I am having is when displaying the cart totals in the cart as well as during checkout. As an example, the application sends the template the prices to display in the shopping cart:

小计:9.75

发货:5.95

总计:15.70

subtotal: 9.75
ship: 5.95
total: 15.70

模板将使用这些金额并在每个项目上调用priceDisplay函数。如果货币汇率为1.1,则向用户显示:

The template takes these amounts and calls the priceDisplay function on each item. If the currency rate is 1.1, then we get displayed to the user:

小计:10.725-> 10.73

船:6.545-> 6.55 < br>
总计:17.27

subtotal: 10.725 -> 10.73
ship: 6.545 -> 6.55
total: 17.27

您可以看到小计+船= 17.28,但转换后的总数为17.27。

You can see that the subtotal + ship = 17.28, but the total converted is 17.27.

所以我认为可以使用几个选项,尽管并不是一直认为这样:

So couple of options I think could work, though not thought all the way through:


  1. 处理所有转换应用程序端

  2. 要汇总项目的位置,模板应将所有单个加数和总计以基本货币形式一起发送到priceDisplay函数,该函数将对它们进行转换并确保已转换的总数加数之和匹配。在这种情况下,那么我该如何与应用程序通信,总数不是15.70,而是15.71或15.69(因为我们将以基本货币存储订单,并在处理付款时乘以exchangeRate。)
  3. b $ b
  4. 在转换过程中跟踪下降/增加的小数点,并对此进行智能处理。因此,在此示例10.725中,我们增加了千分之一。因此,当我们转换6.545时,我们应该先删除.005,然后再转换。也许这就是上面选项2的过程?

  5. 您在这里的建议。

  1. Handle all conversion on the application side
  2. Where items will be totaled, the template should send all the individual addends and total together in the base currency to the priceDisplay function, which will convert them and ensure that the converted total and sum of addends match up. In this case, then how do I communicate to the application that the total is not 15.70 but perhaps 15.71 or 15.69 (since we will be storing the order in base currency and multiply by the exchangeRate when processing the payment.)
  3. Keep track of dropped/added decimal points as part of the conversions and do something 'smart' with that. So in this example, 10.725, we added 5 thousandths. So when we convert 6.545, we should first drop .005 then convert. Perhaps this is the process that option 2 above does?
  4. Your suggestion here.

如果

您还可以在添加购物车项目的行总数时看到相同的问题:

3个项目x每个9.75 = 29.25

转换为:

3个项目x 10.73(10.725)= 32.18(32.175)

但是3 x 10.73 = 32.19!= 32.18

You can also see the same issue in adding the line totals of the cart items:
3 items x 9.75 each = 29.25
converted:
3 items x 10.73 (10.725) = 32.18 (32.175)
but 3 x 10.73 = 32.19 != 32.18

推荐答案

我坚定地站在第一阵营。货币转换是核心业务逻辑,属于您的模型,而不是您的视图。

I'm firmly in the 1 camp. Currency conversion is core business logic, and belongs in your models, not your views.

尽管货币看起来像浮点数,但不是。无论您的基本单位是多少,金钱都会以整数形式易手。例如,在美国,如果口香糖每个10美分,而我买十个,那么我将100便士换成10个口香糖。即使我给了美元钞票,从软件的角度来看,最好也算为100便士。

Further, although money looks like floating-point numbers, it isn't. Money changes hands in integer quantities of whatever your base unit is. In the US, for example, If gumballs are 10 cents each and I buy ten, then I'm trading 100 pennies for 10 gumballs. Even if I give a dollar bill, from a software perspective, it's best to count that as 100 pennies.

有关此的更多信息,请参阅Martin Fowler的企业应用程序体系结构模式 分析模式 。他详细讨论了所有问题,并提供了很好的示例代码。您还可以在网络上找到一些此类信息:

For more on this, see Martin Fowler's "Patterns of Enterprise Application Architecture" and "Analysis Patterns". He talks through all the issues in detail and gives good sample code. You can also find some of that info on the web:


  • 数量(带有一些钱示例代码)

  • 金钱(主要是他书中的指针)

  • Quantity (with some money sample code)
  • Money (mainly a pointer into his book)

如果您需要记帐上班,我还要和会计师谈谈。汇率转换通常会因汇率,怪异费用和其他胡说八道而变得复杂,如果您一开始无法正确使用货币,您可能会花费很长的时间来追逐便士,以保持账面余额。

If you need the accounting to work, I'd also talk to the accountant. Currency conversion is often complicated by changing rates, weird fees, and other nonsense, and you can spend a long time chasing pennies to make the books balance if you don't get it right from the get-go.

这篇关于电子商务网站的货币转换-防止由于舍入而导致的总购物车错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆