我有一个我不能自己解决的问题,我想我可以在php和mysql中得到最精确的计算增值税价格的方法。
例如:
I have the gross price = 1199
The TAX Amount is = 24
to extract the vat = 1199 / 1.24 = 966.93548387096 round(966.93548387096,2) = 966.94
ok
now this value will be saved in mysql with decimal 12,2
Now, if you add the vat = 966.94 * 24 / 100 = 232.0656 round(232.0656,2) = 232.07
Final = 966.94 + 232.07 = 1199.01我的问题是,我如何才能在不浮动的情况下获得1199?
任何帮助都会被感谢。!
更新:
保存在数据库中的值是966.94,从那里开始应用增值税金额。
当我在数据库中引入价格时,一种选择是用4个小数来保存值,但为此,我需要覆盖一个数组来提取列并取出vat。
像这样:
$arrResult = array();
$handle = fopen("uploads/csv/".$csv, "r");
if( $handle ) {
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$arrResult[] = $row;
}
fclose($handle);
}
$titles = array_shift($arrResult);
// need to take out the price and apply price / 1.24 and save the value as 4 decimal in mysql
$keys = array('code', 'price');
$final = array();
foreach ( $arrResult as $key => $value ) {
$final[] = array_combine($keys, $value);
}发布于 2013-01-23 00:05:04
您可以使用MySQL的CAST()函数。
示例:
mysql> select 1199.01 as floating, cast( 1199.01 as signed ) as non_floating;
+----------+--------------+
| floating | non_floating |
+----------+--------------+
| 1199.01 | 1199 |
+----------+--------------+
1 row in set (0.00 sec)请参阅
CAST can be used to convert to the following types:
1. BINARY[(N)]
2. CHAR[(N)] DATE
3. DATETIME DECI
4. MAL[(M[,D])]
5. SIGNED [INTEGER]
6. TIME
7. UNSIGNED [INTEGER]发布于 2020-11-29 02:55:09
这是一个古老的问题,但你永远不应该使用浮点数来表示货币金额。
这是因为floats在计算上造成了很多麻烦(如果你在网上搜索,你会发现大量解释这个问题的文章)。
因此,解决方案是使用像https://github.com/moneyphp/money这样的值对象
然后,您将在数据库中保存基本金额,它是一个以最小单位表示的简单整数。
这意味着100,00欧元表示为10000美分。
这个库只对整数进行操作(并且在内部使用字符串),非常非常精确!
https://stackoverflow.com/questions/14462515
复制相似问题