Skip to content

Commit 574583b

Browse files
committed
Do not round values of parameters, we can now use the full double precision
This fixes issue #681
1 parent 84c54d0 commit 574583b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Form/Type/ExponentialNumberType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Form\AbstractType;
2828
use Symfony\Component\Form\Extension\Core\Type\NumberType;
2929
use Symfony\Component\Form\FormBuilderInterface;
30+
use Symfony\Component\OptionsResolver\OptionsResolver;
3031

3132
/**
3233
* Similar to the NumberType, but formats small values in scienfitic notation instead of rounding it to 0, like NumberType
@@ -38,6 +39,14 @@ public function getParent(): string
3839
return NumberType::class;
3940
}
4041

42+
public function configureOptions(OptionsResolver $resolver)
43+
{
44+
$resolver->setDefaults([
45+
//We want to allow the full precision of the number, so disable rounding
46+
'scale' => null,
47+
]);
48+
}
49+
4150
public function buildForm(FormBuilderInterface $builder, array $options)
4251
{
4352
$builder->resetViewTransformers();

src/Form/Type/Helper/ExponentialNumberTransformer.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
class ExponentialNumberTransformer extends NumberToLocalizedStringTransformer
3434
{
3535
public function __construct(
36-
protected ?int $scale = null,
36+
private ?int $scale = null,
3737
?bool $grouping = false,
3838
?int $roundingMode = \NumberFormatter::ROUND_HALFUP,
3939
protected ?string $locale = null
4040
) {
41+
//Set scale to null, to disable rounding of values
4142
parent::__construct($scale, $grouping, $roundingMode, $locale);
4243
}
4344

@@ -85,12 +86,28 @@ protected function getScientificNumberFormatter(): \NumberFormatter
8586
$formatter = new \NumberFormatter($this->locale ?? \Locale::getDefault(), \NumberFormatter::SCIENTIFIC);
8687

8788
if (null !== $this->scale) {
88-
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale);
89+
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $this->scale);
8990
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
9091
}
9192

9293
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, (int) $this->grouping);
9394

95+
return $formatter;
96+
}
97+
98+
protected function getNumberFormatter(): \NumberFormatter
99+
{
100+
$formatter = parent::getNumberFormatter();
101+
102+
//Unset the fraction digits, as we don't want to round the number
103+
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0);
104+
if (null !== $this->scale) {
105+
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $this->scale);
106+
} else {
107+
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 100);
108+
}
109+
110+
94111
return $formatter;
95112
}
96113
}

0 commit comments

Comments
 (0)