在PHP

时间:2018-01-09 15:41:17

标签: php

我有一个表格,当用户填写他们拥有的瓶子总数时,它会插入到数据库中,然后应该总结出有多少个案例。

例如在葡萄酒中 - 有12个瓶子,如果用户放入100瓶,它应该除以12并给出总和8.33333333。

$bottles = "100";

最好的方法是将这个数字减少到8号,然后算出剩下多少瓶从未成为完整的案例?

希望这是有道理的。

3 个答案:

答案 0 :(得分:6)

您可以使用floor

$bottles = "100";
$case = floor( $bottles / 12 );

echo $case;

将导致 8

文档:http://php.net/manual/en/function.floor.php

如果您想检查剩余的瓶子,可以使用模数

$bottles = "100";
$left = $bottles % 12;

将导致 4

答案 1 :(得分:1)

您可以使用floor向下舍入,使用模(%)运算符来确定剩余的瓶数。

$bottles = 100;
$bottles_per_case = 12;

print "There are " . floor($bottles / $bottles_per_case) . " cases...";
print "With " . ($bottles % $bottles_per_case) . " bottles left over";

答案 2 :(得分:0)

$bottles = "100";
$case = (int) $bottles / 12 ;
echo $case;
$left = $bottles % 12;
echo '<br>left: ' . $left;