class Percentage

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Batch/Percentage.php \Drupal\Core\Batch\Percentage
  2. 10 core/lib/Drupal/Core/Batch/Percentage.php \Drupal\Core\Batch\Percentage
  3. 11.x core/lib/Drupal/Core/Batch/Percentage.php \Drupal\Core\Batch\Percentage

Helper methods for the batch system.

Hierarchy

Expanded class hierarchy of Percentage

2 files declare their use of Percentage
batch.inc in core/includes/batch.inc
Batch processing API for processes to run in multiple HTTP requests.
PercentagesTest.php in core/tests/Drupal/Tests/Core/Batch/PercentagesTest.php

File

core/lib/Drupal/Core/Batch/Percentage.php, line 8

Namespace

Drupal\Core\Batch
View source
class Percentage {
    
    /**
     * Formats the percent completion for a batch set.
     *
     * @param int $total
     *   The total number of operations.
     * @param int $current
     *   The number of the current operation. This may be a floating point number
     *   rather than an integer in the case of a multi-step operation that is not
     *   yet complete; in that case, the fractional part of $current represents the
     *   fraction of the operation that has been completed.
     *
     * @return string
     *   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.
     *
     * @see _batch_process()
     */
    public static function format($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);
            do {
                // Calculate the percentage to the specified number of decimal places.
                $percentage = sprintf('%01.' . $decimal_places . 'f', round($current / $total * 100, $decimal_places));
                // When $current is an integer, the above calculation will always be
                // correct. However, if $current is a floating point number (in the case
                // of a multi-step batch operation that is not yet complete), $percentage
                // may be erroneously rounded up to 100%. To prevent that, we add one
                // more decimal place and try again.
                $decimal_places++;
            } while ($percentage == '100');
        }
        return $percentage;
    }

}

Members

Title Sort descending Modifiers Object type Summary
Percentage::format public static function Formats the percent completion for a batch set.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.