[英]PHP float number with decimal point rounding
I am calculating few numbers (sales_total, service_charge) to get 7% GST.我正在计算几个数字(sales_total、service_charge)以获得 7% 的消费税。 What I get is number with decimal point.
我得到的是带小数点的数字。
Example 1:示例 1:
$sales_total = 207.50;
$service_charge = 20.75;
$gst = ($sales + $sc) * .07;
Returns me GST = 15.9775
.返回给我 GST =
15.9775
。
Example 2:示例 2:
$sales_total = 28;
$service_charge = 2.8;
$gst = ($sales + $sc) * .07;
Returns me GST = 2.156
2.156
我 GST = 2.156
I need to match the data with a report where the我需要将数据与报告匹配,其中
I am trying php round
, number_format
functions but not getting both of my results correct.我正在尝试 php
round
、 number_format
函数,但没有让我的两个结果都正确。 round
gives example 1 result correct, number_format
gives example 2 correct. round
给出示例 1 结果正确, number_format
给出示例 2 正确。
What I am doing wrong, or which function I need to use?我做错了什么,或者我需要使用哪个功能?
Have you tried:你有没有尝试过:
round($gst, 2);
this will round your variable within 2 decimal houses.这会将您的变量四舍五入到 2 个小数宫内。
Could you try?你可以试试吗?
Since you want different behaviors in both values I don't think that round can help you in both situations.由于您希望在两种价值观中都有不同的行为,因此我认为该回合无法在两种情况下为您提供帮助。 Actually the second example you don't even want a round.
实际上第二个例子你甚至不想要一轮。 You just want to delete the 3rd decimal number so...
你只想删除第三个十进制数,所以......
In this case I would use the 1st example the round with 2 decimal houses as I said and in the 2nd example I would just delete the 3rd house number or limit in the database>table the float number to 2 decimal houses.在这种情况下,我将使用第一个示例,如我所说的带有 2 个小数房屋的回合,在第二个示例中,我将删除第三个房屋号码或将数据库中的限制>表格浮点数设为 2 个小数房屋。
I believe you're probably needing to round with precision decimal places away from zero, so depending on that you could use:我相信您可能需要使用远离零的精确小数位四舍五入,因此取决于您可以使用:
$gst = sprintf("%.2f", round($gst, 3 - strlen(round($gst)), PHP_ROUND_HALF_ODD));
Result:结果:
16.00
2.16
This shows a precision of 3 decimal places away from zero minus the length of the rounded value.这显示了 3 个小数位的精度,距零减去舍入值的长度。 You'll likely need to play around with the rounding mode and precision to get it how you want.
您可能需要使用舍入模式和精度来获得您想要的效果。 The result of
2.15
from 2.156
looks incorrect — it's unclear how you've gotten this value.来自
2.156
的2.15
的结果看起来不正确——不清楚你是如何得到这个值的。
PHP_ROUND_HALF_UP • Round val up to precision decimal places away from zero, when it is half way there.
PHP_ROUND_HALF_UP • 将 val 向上舍入到远离零的精确小数位,当它是一半时。 Making 1.5 into 2 and -1.5 into -2.
将 1.5 变成 2,将 -1.5 变成 -2。
PHP_ROUND_HALF_DOWN • Round val down to precision decimal places towards zero, when it is half way there.
PHP_ROUND_HALF_DOWN • 将 val 向下舍入到接近零的精确小数位,当它达到一半时。 Making 1.5 into 1 and -1.5 into -1.
将 1.5 变为 1,将 -1.5 变为 -1。
PHP_ROUND_HALF_EVEN • Round val to precision decimal places towards the next even value.
PHP_ROUND_HALF_EVEN • 将val舍入到下一个偶数值的精确小数位。
PHP_ROUND_HALF_ODD • Round val to precision decimal places towards the next odd value.
PHP_ROUND_HALF_ODD • 将val舍入到下一个奇数值的精确小数位。
Reference: http://php.net/manual/en/function.round.php参考: http : //php.net/manual/en/function.round.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.