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

Appearance settings

Commit fe6aa64

Browse filesBrowse files
committed
feature #26767 [Form] ability to set rounding strategy for MoneyType (syastrebov)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Form] ability to set rounding strategy for MoneyType | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | no | License | MIT | Doc PR | symfony/symfony-docs#9543 Added `rounding_mode` to the `MoneyType` to be possible to change rounding strategy for money values. For now it's just `ROUND_HALF_UP` but it's good to have `ROUND_DOWN` as well. E.g. to transform `15.999` to `15.99` instead of `15.1`. Commits ------- f3b1424 rounding_mode for money type
2 parents e973f6f + f3b1424 commit fe6aa64
Copy full SHA for fe6aa64

File tree

Expand file treeCollapse file tree

3 files changed

+30
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+30
-1
lines changed

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added `input=datetime_immutable` to DateType, TimeType, DateTimeType
8+
* added `rounding_mode` option to MoneyType
89

910
4.0.0
1011
-----

‎src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
1516
use Symfony\Component\Form\FormInterface;
1617
use Symfony\Component\Form\FormBuilderInterface;
1718
use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
@@ -31,7 +32,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3132
->addViewTransformer(new MoneyToLocalizedStringTransformer(
3233
$options['scale'],
3334
$options['grouping'],
34-
null,
35+
$options['rounding_mode'],
3536
$options['divisor']
3637
))
3738
;
@@ -53,11 +54,22 @@ public function configureOptions(OptionsResolver $resolver)
5354
$resolver->setDefaults(array(
5455
'scale' => 2,
5556
'grouping' => false,
57+
'rounding_mode' => NumberToLocalizedStringTransformer::ROUND_HALF_UP,
5658
'divisor' => 1,
5759
'currency' => 'EUR',
5860
'compound' => false,
5961
));
6062

63+
$resolver->setAllowedValues('rounding_mode', array(
64+
NumberToLocalizedStringTransformer::ROUND_FLOOR,
65+
NumberToLocalizedStringTransformer::ROUND_DOWN,
66+
NumberToLocalizedStringTransformer::ROUND_HALF_DOWN,
67+
NumberToLocalizedStringTransformer::ROUND_HALF_EVEN,
68+
NumberToLocalizedStringTransformer::ROUND_HALF_UP,
69+
NumberToLocalizedStringTransformer::ROUND_UP,
70+
NumberToLocalizedStringTransformer::ROUND_CEILING,
71+
));
72+
6173
$resolver->setAllowedTypes('scale', 'int');
6274
}
6375

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/MoneyTypeTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,20 @@ public function testMoneyPatternWithoutCurrency()
7070

7171
$this->assertSame('{{ widget }}', $view->vars['money_pattern']);
7272
}
73+
74+
public function testDefaultFormattingWithDefaultRounding()
75+
{
76+
$form = $this->factory->create(static::TESTED_TYPE, null, array('scale' => 0));
77+
$form->setData('12345.54321');
78+
79+
$this->assertSame('12346', $form->createView()->vars['value']);
80+
}
81+
82+
public function testDefaultFormattingWithSpecifiedRounding()
83+
{
84+
$form = $this->factory->create(static::TESTED_TYPE, null, array('scale' => 0, 'rounding_mode' => \NumberFormatter::ROUND_DOWN));
85+
$form->setData('12345.54321');
86+
87+
$this->assertSame('12345', $form->createView()->vars['value']);
88+
}
7389
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.