Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何计算php货币中的便士?_Php - Fatal编程技术网

我如何计算php货币中的便士?

我如何计算php货币中的便士?,php,Php,我有一个变量,名为 $total_cost 等于529700 我需要的价值是5297.00美元 此值来自mysql十进制10,2字段 我现在需要根据用户是否处于该状态,得出2%的小计和8.25%的发货 让我们假设所有的用户都来自这个状态,我如何通过php来保存这些数据呢 例如,这就是我所想的 $subtotal = $total_cost * 0.02; $shipping = $total_cost * 0.0825; $new_total = $total_cost + $shipping

我有一个变量,名为

$total_cost
等于529700

我需要的价值是5297.00美元

此值来自mysql十进制10,2字段

我现在需要根据用户是否处于该状态,得出2%的小计和8.25%的发货

让我们假设所有的用户都来自这个状态,我如何通过php来保存这些数据呢

例如,这就是我所想的

$subtotal = $total_cost * 0.02;
$shipping = $total_cost * 0.0825;
$new_total = $total_cost + $shipping + $subtotal;
但是当我打印这张照片的时候,我得到了0美元……你知道为什么吗

<?php print "$". $new_total; ?>

您需要查看此代码的分解位置,以及您认为您拥有的值是否实际存在。。。。这个输出是什么样子的

echo "Total: ".$total_cost."<br />";

$subtotal = $total_cost * 0.02;
echo "Sub: ".$subtotal . "<br />";

$shipping = $total_cost * 0.0825;
echo "Shipping: ".$shipping . "<br />";

$new_total = $total_cost + $shipping + $subtotal;
echo "New Total: ".$new_total . "<br />";
echo“总计:.$Total_成本。”
; $小计=$总成本*0.02; echo“Sub:”.$小计。“
”; $shipping=$total_cost*0.0825; echo“Shipping:”.$Shipping。“
”; $new_total=$total_cost+$shipping+$subtotal; echo“新总计:”.$New_总计。“
”;
我在测试代码时没有得到这个结果

php > $total_cost=529700;
php > $subtotal = $total_cost * 0.02;
php > $shipping = $total_cost * 0.0825;
php > $new_total = $total_cost + $shipping + $subtotal;
php > print "$". $new_total;
$583994.25
您确定初始值是您期望的值吗

看起来它可能会对你有所帮助。抓取数字,除以100,进行计算,并在输出时格式化

<?php print "$".number_format($new_total/100, 2); ?>

计算百分比时,必须将其转换为浮点数,或者整数向下舍入为0

$subtotal = $totalcost / 100.0 * 0.02;
$shipping = $totalcost / 100.0 * 0.0825;
$newtotal = $totalcost / 100.0 + $subtotal + $shipping;

@Ignacio:+1表示奥卡姆剃须刀。@Matt-
var\u dump($total\u cost)
$subtotal = $totalcost / 100.0 * 0.02;
$shipping = $totalcost / 100.0 * 0.0825;
$newtotal = $totalcost / 100.0 + $subtotal + $shipping;