php货币转换舍入错误


php currency conversion rounding errors

我有一个数据库,它以GBP(小数点后两位)存储货币金额。偶尔我们会收到美元订单。这些在储存之前使用1.51的汇率进行转换。问题是,当这些数字被检索并转换回原始货币时,我会遇到一些小的舍入错误。例如

$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

关于如何解决这个问题,有什么想法吗?

非常感谢

我曾经遇到过同样的问题。我的值存储在表中。

所以我没有保存2个小数,而是保存了6个,并且在客户端只显示2个