Below is the code I am using within a while loop to display a number within a table row and it's amount divided by 200 right next to it ('amount').
开发者_运维问答It works ok in that it takes off the decimals and divides by 200 but I was wondering how do I round it down?
For example If I have 850 it will echo '4', however if the amount is over 900 it will echo '5'. I gather that if it is over the halfway mark of 200 it will round up, but how can I round down everything that is below 200?
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>';
echo '<td align="left"><strong>' . $row['name'] . '</strong></td> ';
echo '<td align="left">' . $row['amount'] . '</td>';
echo '<td align="center"><strong><font color="#be0f34">';
echo number_format("{$row['amount']}"/200,0);
echo '</font></strong></td>';
echo '<td align="center">' . $row['date'] . '</td>
</tr>
';
}
Cheers
Use floor()
:
echo number_format(floor($row['amount']/200),0);
floor()
is what you want.
You should use floor()
instead of number_format()
echo floor($row['amount']/200);
You shouldn't be forcing a type-conversion to a string in the first argument you're passing to number_format
. Also, the second argument defaults to zero, so you don't need to specify that explicitly. You can just use number_format($row['amount'] / 200);
on that line.
As far as your question is concerned, I'm gathering that you want to round when the amount is above 200 only so that you do not get "1" as a result when the amount is >= 100. To do that, you will need to do something along the lines of this:
$divided_amount = $row['amount'] / 200;
$divided_amount = floor($divided_amount) ? number_format($divided_amount) : '0';
// display code here
That will only number_format
(and thus, round) if the divided amount is >= 200. Otherwise it'll floor (which we know will return zero since we just tested it).
精彩评论