Do not round values of parameters, we can now use the full double precision

This fixes issue #681
This commit is contained in:
Jan Böhmer 2024-09-09 21:33:28 +02:00
parent 84c54d0b25
commit 574583bd6a
2 changed files with 28 additions and 2 deletions

View file

@ -27,6 +27,7 @@ use App\Form\Type\Helper\ExponentialNumberTransformer;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/** /**
* Similar to the NumberType, but formats small values in scienfitic notation instead of rounding it to 0, like NumberType * Similar to the NumberType, but formats small values in scienfitic notation instead of rounding it to 0, like NumberType
@ -38,6 +39,14 @@ class ExponentialNumberType extends AbstractType
return NumberType::class; return NumberType::class;
} }
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
//We want to allow the full precision of the number, so disable rounding
'scale' => null,
]);
}
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder->resetViewTransformers(); $builder->resetViewTransformers();

View file

@ -33,11 +33,12 @@ use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStrin
class ExponentialNumberTransformer extends NumberToLocalizedStringTransformer class ExponentialNumberTransformer extends NumberToLocalizedStringTransformer
{ {
public function __construct( public function __construct(
protected ?int $scale = null, private ?int $scale = null,
?bool $grouping = false, ?bool $grouping = false,
?int $roundingMode = \NumberFormatter::ROUND_HALFUP, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP,
protected ?string $locale = null protected ?string $locale = null
) { ) {
//Set scale to null, to disable rounding of values
parent::__construct($scale, $grouping, $roundingMode, $locale); parent::__construct($scale, $grouping, $roundingMode, $locale);
} }
@ -85,12 +86,28 @@ class ExponentialNumberTransformer extends NumberToLocalizedStringTransformer
$formatter = new \NumberFormatter($this->locale ?? \Locale::getDefault(), \NumberFormatter::SCIENTIFIC); $formatter = new \NumberFormatter($this->locale ?? \Locale::getDefault(), \NumberFormatter::SCIENTIFIC);
if (null !== $this->scale) { if (null !== $this->scale) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale); $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $this->scale);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
} }
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, (int) $this->grouping); $formatter->setAttribute(\NumberFormatter::GROUPING_USED, (int) $this->grouping);
return $formatter;
}
protected function getNumberFormatter(): \NumberFormatter
{
$formatter = parent::getNumberFormatter();
//Unset the fraction digits, as we don't want to round the number
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0);
if (null !== $this->scale) {
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $this->scale);
} else {
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 100);
}
return $formatter; return $formatter;
} }
} }