Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

NumberType does not apply rouding when scale is not defined #20134

Open
Labels
Formhelp wantedIssues and PRs which are looking for volunteers to complete them.
@pacproduct

Description

@pacproduct

Hi :)

I may be wrong, but documentation (Symfony 5.x) says thatNumberType's optionrounding_mode defaults to\NumberFormatter::ROUND_HALFUP :https://symfony.com/doc/5.x/reference/forms/types/number.html#rounding-mode

In my project, one of my form's fields is aNumberType, with norounding_mode norscale configured:

publicfunction buildForm(FormBuilderInterface$builder,array$options)    {$builder            ->add('altitude',                NumberType::class,                ['label' =>'common.label.altimetric_reference.altitude','attr' => ['class' =>'altitude-field'],'required' =>false,'help' =>'common.help.value_in_m',                ]            )[...]

But because of how\Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer::getNumberFormatter has been implemented, I think the documentation is not accurate:

protectedfunctiongetNumberFormatter()    {$formatter =new \NumberFormatter($this->locale ?? \Locale::getDefault(), \NumberFormatter::DECIMAL);if (null !==$this->scale) {$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS,$this->scale);$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE,$this->roundingMode);        }$formatter->setAttribute(\NumberFormatter::GROUPING_USED,$this->grouping);return$formatter;    }

As shown above, the number formatter is not configured with any rounding when no scale is defined (even if one is configured in the form I reckon). Because of that, in our project (with French locale), the effective rounding mode used is\NumberFormatter::ROUND_HALFEVEN (and scale seems to default to3). Instead of the advertised\NumberFormatter::ROUND_HALFUP.

This gives us an unexpected and weird behavior when users type in numbers like485,3485, as it gets rounded to485,348 instead of485,349.

In all honesty, I'm not sure if this is a documentation issue, a bug inNumberToLocalizedStringTransformer::getNumberFormatter or if I'm missing something?

Thanks for your insights :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Formhelp wantedIssues and PRs which are looking for volunteers to complete them.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp