diff --git a/core/lib/Drupal/Component/Utility/Number.php b/core/lib/Drupal/Component/Utility/Number.php index 2aadf54123..7cc19b9214 100644 --- a/core/lib/Drupal/Component/Utility/Number.php +++ b/core/lib/Drupal/Component/Utility/Number.php @@ -27,7 +27,8 @@ class Number { * BCMath (https://secure.php.net/manual/en/book.bc.php). * * @param $number int|float|string - * The value to normalize. + * The value to normalize. If this is a string, it must be formatted as an + * integer or a float. * * @return string * The normalized string containing integers with an optional decimal @@ -48,21 +49,22 @@ public static function normalize($number) { * Counts a number's significant decimals. * * @param int|float|string - * The number as a numeric type or a decimal string. + * The number whose decimals to count. If this is a string, it must be + * formatted as an integer or a float. * * @return int */ - public static function countSignificantDecimels($number) { + public static function countSignificantDecimals($number) { $number = static::normalize($number); // If no decimal separator is encountered, the step is an integer and the - // precision is 0 (decimals). + // there are 0 significant decimals. if (strrpos($number, '.') === FALSE) { return 0; } - // If a decimal separator is encountered, the precision is the number of - // following digits. + // If a decimal separator is encountered, count the number of significant + // decimal digits. return strlen($number) - strrpos($number, '.') - 1; } @@ -82,9 +84,11 @@ public static function countSignificantDecimels($number) { * validity. * * @param int|float|string $value - * The value that needs to be checked. + * The value that needs to be checked. If this is a string, it must be + * formatted as an integer or a float. * @param int|float|string $step - * The step scale factor. Must be positive. + * The step scale factor. Must be positive. If this is a string, it must be + * formatted as an integer or a float. * @param float $offset * (optional) An offset, to which the difference must be a multiple of the * given step. @@ -97,18 +101,17 @@ public static function countSignificantDecimels($number) { public static function validStep($value, $step, $offset = 0.0) { $float_value = (float) abs($value - $offset); - // Desired precision of comparison is one order of magnitude greater than - // the precision of the step. - $desired_precision = static::countSignificantDecimels($step) + 1; + // The expected number significant decimals is dictated by the step. + $expected_significant_decimals = static::countSignificantDecimals($step) + 1; - // If the value is of higher precision than desired it isn't divisible by - // step. - $value_precision = static::countSignificantDecimels((string) $float_value); - if ($value_precision > $desired_precision) { + // If the actual value has more significant decimals than expected, it has a + // higher precision than desired it isn't divisible by the step. + $actual_significant_decimals = static::countSignificantDecimals((string) $float_value); + if ($actual_significant_decimals > $expected_significant_decimals) { return FALSE; } - $float_value = (float) round($float_value, $desired_precision); + $float_value = (float) round($float_value, $expected_significant_decimals); // The fractional part of a double has 53 bits. The greatest number that // could be represented with that is 2^53. If the given value is even bigger @@ -120,7 +123,7 @@ public static function validStep($value, $step, $offset = 0.0) { return TRUE; } - $expected_float_value = (float) round($step * round($float_value / $step), $desired_precision); + $expected_float_value = (float) round($step * round($float_value / $step), $expected_significant_decimals); // Now compute that remainder of a division by $step. $remainder = (float) abs($float_value - $expected_float_value); diff --git a/core/tests/Drupal/Tests/Component/Utility/NumberTest.php b/core/tests/Drupal/Tests/Component/Utility/NumberTest.php index 4a40fcc75a..eb4658c94b 100644 --- a/core/tests/Drupal/Tests/Component/Utility/NumberTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/NumberTest.php @@ -35,7 +35,7 @@ public function testValidStep($value, $step, $expected) { } /** - * @covers ::countSignificantDecimels + * @covers ::countSignificantDecimals * * @dataProvider provideGetPrecision * @@ -45,7 +45,7 @@ public function testValidStep($value, $step, $expected) { * The number to test the precision computation on. */ public function testGetPrecision($expected, $number) { - $this->assertEquals($expected, Number::countSignificantDecimels($number)); + $this->assertEquals($expected, Number::countSignificantDecimals($number)); } /**