[Form] Improve rounding precision
This commit is contained in:
parent
a786b5aaaf
commit
e50804cef4
@ -266,7 +266,8 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
|
||||
if (null !== $this->precision && null !== $this->roundingMode) {
|
||||
// shift number to maintain the correct scale during rounding
|
||||
$roundingCoef = pow(10, $this->precision);
|
||||
$number *= $roundingCoef;
|
||||
// string representation to avoid rounding errors, similar to bcmul()
|
||||
$number = (string) ($number * $roundingCoef);
|
||||
|
||||
switch ($this->roundingMode) {
|
||||
case self::ROUND_CEILING:
|
||||
|
@ -307,6 +307,8 @@ class NumberToLocalizedStringTransformerTest extends TestCase
|
||||
array(1, '123,44', 123.4, NumberToLocalizedStringTransformer::ROUND_DOWN),
|
||||
array(1, '-123,45', -123.4, NumberToLocalizedStringTransformer::ROUND_DOWN),
|
||||
array(1, '-123,44', -123.4, NumberToLocalizedStringTransformer::ROUND_DOWN),
|
||||
array(2, '37.37', 37.37, NumberToLocalizedStringTransformer::ROUND_DOWN),
|
||||
array(2, '2.01', 2.01, NumberToLocalizedStringTransformer::ROUND_DOWN),
|
||||
// round halves (.5) to the next even number
|
||||
array(0, '1234,6', 1235, NumberToLocalizedStringTransformer::ROUND_HALF_EVEN),
|
||||
array(0, '1234,5', 1234, NumberToLocalizedStringTransformer::ROUND_HALF_EVEN),
|
||||
|
Reference in New Issue
Block a user