简体   繁体   English

如何在 php 中显示整数?

[英]How to show round figure number in php?

I have a variable called $totalResults which has dynamic values, depend on database entries.我有一个名为$totalResults的变量,它具有动态值,取决于数据库条目。 I need to show a limit like if $totalResults has 154 results then I want show that 154 out of 500. But I want it dynamically, like if the result cross 500, example the result value is 514, then it'll show like 514 out of 1000.我需要显示一个限制,例如$totalResults154结果,然后我想显示 500 个中的 154 个。但我希望它是动态的,例如如果结果超过 500,例如结果值为 514,那么它将显示为 514出1000。

I don't want to use if, else conditions because the result will gradually increase, so i need to compare it dynamically, Like if result has 1500 entries, then I'll add +500 into it, Then it'll show Like 1500 out of 2000.我不想用if,else条件,因为结果会逐渐增加,所以我需要动态比较,比如如果结果有1500个条目,那么我会在里面加上+500,然后它会显示Like 1500 2000 之外。

I am confused in this part, If the result is not round figure, how can I make it right?我在这部分很困惑,如果结果不是圆形,我怎么能把它弄好?

My Logic:我的逻辑:

$totalResult = 154;
$newResult = $totalResult + 500;

//Result = 654
But I want to show this 154 out of 500 or 154 out of 1000

You can use the ceil function您可以使用ceil function

$totalResult =154;
$coparisonQuotient = ceil($totalresults/500);
$outOf = $comparisonQuotient * 500;

echo $totalResult. " out of " .$outOf;

Now, if the total results is 154, it would display 154 out of 500现在,如果总结果是 154,它将显示154 out of 500

If it is above 500, but less than 1000, let's say 784. It will display 784 out of 1000如果高于 500 但低于 1000,假设为 784。它将显示784 out of 1000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM