| Versionen | |
|---|---|
| drupal7 | _batch_api_percentage($total, $current) |
Helper function for _batch_process(): returns the formatted percentage.
$total The total number of operations.
$current The number of the current operation.
The properly formatted percentage, as a string. We output percentages using the correct number of decimal places so that we never print "100%" until we are finished, but we also never print more decimal places than are meaningful.
includes/
<?php
function _batch_api_percentage($total, $current) {
if (!$total || $total == $current) {
// If $total doesn't evaluate as true or is equal to the current set, then
// we're finished, and we can return "100".
$percentage = "100";
}
else {
// We add a new digit at 200, 2000, etc. (since, for example, 199/200
// would round up to 100% if we didn't).
$decimal_places = max(0, floor(log10($total / 2.0)) - 1);
$percentage = sprintf('%01.' . $decimal_places . 'f', round($current / $total * 100, $decimal_places));
}
return $percentage;
}
?>
Kommentare
Kommentar hinzufügen