简体   繁体   English

PHP货币换算舍入错误

[英]php currency conversion rounding errors

I have a database which stores currency amounts in GBPs (to two decimal places). 我有一个数据库,以GBPs(到小数点后两位)存储货币金额。 Occasionally we get orders in in USD. 有时我们会收到美元订单。 These are converted prior to storage using an exchange rate of 1.51. 这些在存储之前使用1.51的汇率进行转换。 The problem is when these figures are retrieved and converted back to the original currency I am encountering small rounding errors. 问题是当检索这些数字并将其转换回原始货币时,我遇到小的取整错误。 For example... 例如...

$amount = 100 ///figure in USD
$gbpAmount = number_format((float)($amount/1.51), 2, '.', ''); //// gives GBP figure of 66.23

////Store this in dB then retrive////

$usdAmount = number_format((float)($gbpAmount*1.51), 2, '.', ''); //// gives USD figure of 100.01

Any ideas on how I can resolve this problem? 关于如何解决此问题的任何想法?

Many thanks 非常感谢

I once faced the same issue. 我曾经遇到过同样的问题。 My values where stored in a table. 我的值存储在表中。

So instead of saving 2 decimal, I saved 6 and shows only 2 in the client side 因此,我没有保存2个小数,而是保存了6个并在客户端仅显示2个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM