我有一个包含文章列表的网站。我想包含一行,说明数据库中有多少篇文章,但是我不希望它显示确切的计数。
我希望它向下舍入到最接近的 10,例如,对于 105 篇文章,我希望它说“100+”等。
我该怎么做呢?
要执行类似的操作,您必须使用底数 = 10 的对数并将其四舍五入
$exp= floor(log($num)/log(10));
$num = pow(10,$exp)."+";
这适用于 10、100、1000 ecc,我认为按照您的要求做更好。
$count = 105;
$nearest10 = floor($count / 10) * 10;
printf("%d0+", $count * 0.1);
或者
echo substr($count, 0, -1), '0+';
round
Docs功能也支持开箱即用:
round($count, -1, PHP_ROUND_HALF_DOWN);
但加号不见了。
首先,您需要使用COUNT()
MySQL 的 Aggregate 函数来获取结果总数,或者类似的东西。
然后,您需要使用%
以 10 为基数的模运算符 ( ),然后从主要结果中减去该值,如下所示:-
$sql = "SELECT COUNT(id) AS `total_num_records` FROM `table`";
$resource = mysql_query($sql);
$result = mysql_fetch_array($resource);
$totalRecords = $result['total_num_records'];
if ($totalRecords > 0) {
$remainder = $totalRecords % 10;
$showHumanFriendlyRecords = ($totalRecords - $remainder);
$strHumanFriendlyRecords = $showHumanFriendlyRecords . "+";
}
希望能帮助到你。