php - 我如何将小数限制为两位
问题描述
大家好,我希望你能帮助我。
我有'value' => $amount_to_pay
并且我希望我的 $amount_to_pay 乘以 0.11,但输出仅限于小数点后 2 位。它适用于贝宝。
我使用过'value' => $amount_to_pay*0.11
,但小数点后的内容很长,并且从 PayPal exp: 589.24*0.11 = 64.8164 返回错误我希望输出为 64.81
谢谢
解决方案
您可以使用round()
N 小数四舍五入
'value' => round($amount_to_pay * 0.11, 2) // 64.82
正如@PrestonPHX 提到的那样,它给出了一个整数值。如果你想“截断小数,你可以使用:
'value' => (int)($amount_to_pay * 100) / 100 // 64.81
要使用动态小数位数,您可以使用:
$nb = 2; // number of decimals
$exp = pow(10, $nb);
echo (int)($amount_to_pay * $exp) / $exp;
推荐阅读
- javascript - PHP:在 echo 输出函数名称中调用 javascript 函数
- ti-basic - 有没有办法检查变量在 ti-basic 范围内出现的次数?(对于因子计算器)
- php - 使用 AJAX 和 PHP 处理 html 表单中的多个输入字段
- asp.net-core - 如何通过 ASP.NET Core 3 中的中间 Web API 传递未更改的 HTTP 响应?
- excel - Excel 复选框和数组
- c++ - 比较c++中的两个变量
- php - 修复这个 multidimosenal 数组 foreach 循环选择,我做了函数但不起作用,谁能帮我解决这个问题?
- python - Python中的除法错误
- google-sheets - 如何根据多个行标准计算 ArrayFormula 内的加权平均值?
- typescript - 如何使用 typscript 编译器向 typescript 文件中的现有 json 对象添加另一个属性?