Skip to content

Commit 0581218

Browse files
committed
bug #21769 [Form] Improve rounding precision (foaly-nr1)
This PR was squashed before being merged into the 2.7 branch (closes #21769). Discussion ---------- [Form] Improve rounding precision | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | 21734 | License | MIT | Doc PR | For full details see #21734 from #21734 (comment) onwards. Excerpt: ```php $number = floor(37.37*100); // double(3736) var_dump($number); $number = floor(bcmul(37.37, 100)); // double(3737) var_dump($number); ``` From http://php.net/manual/en/language.types.float.php#language.types.float: > Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118.... > > So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available. Commits ------- e50804c [Form] Improve rounding precision
2 parents a786b5a + e50804c commit 0581218

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ private function round($number)
266266
if (null !== $this->precision && null !== $this->roundingMode) {
267267
// shift number to maintain the correct scale during rounding
268268
$roundingCoef = pow(10, $this->precision);
269-
$number *= $roundingCoef;
269+
// string representation to avoid rounding errors, similar to bcmul()
270+
$number = (string) ($number * $roundingCoef);
270271

271272
switch ($this->roundingMode) {
272273
case self::ROUND_CEILING:

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ public function reverseTransformWithRoundingProvider()
307307
array(1, '123,44', 123.4, NumberToLocalizedStringTransformer::ROUND_DOWN),
308308
array(1, '-123,45', -123.4, NumberToLocalizedStringTransformer::ROUND_DOWN),
309309
array(1, '-123,44', -123.4, NumberToLocalizedStringTransformer::ROUND_DOWN),
310+
array(2, '37.37', 37.37, NumberToLocalizedStringTransformer::ROUND_DOWN),
311+
array(2, '2.01', 2.01, NumberToLocalizedStringTransformer::ROUND_DOWN),
310312
// round halves (.5) to the next even number
311313
array(0, '1234,6', 1235, NumberToLocalizedStringTransformer::ROUND_HALF_EVEN),
312314
array(0, '1234,5', 1234, NumberToLocalizedStringTransformer::ROUND_HALF_EVEN),

0 commit comments

Comments
 (0)