Skip to content

Commit

Permalink
[Form] added support for rounding-mode option on NumberFields
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Turner authored and fabpot committed Sep 25, 2010
1 parent 2052501 commit a305eb0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/NumberField.php
Expand Up @@ -27,10 +27,12 @@ protected function configure()
// default precision is locale specific (usually around 3)
$this->addOption('precision');
$this->addOption('grouping', false);
$this->addOption('rounding-mode', NumberToLocalizedStringTransformer::ROUND_HALFUP);

$this->setValueTransformer(new NumberToLocalizedStringTransformer(array(
'precision' => $this->getOption('precision'),
'grouping' => $this->getOption('grouping'),
'rounding-mode' => $this->getOption('rounding-mode'),
)));
}

Expand Down
Expand Up @@ -13,13 +13,22 @@
*/
class NumberToLocalizedStringTransformer extends BaseValueTransformer
{
const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR;
const ROUND_DOWN = \NumberFormatter::ROUND_DOWN;
const ROUND_HALFDOWN = \NumberFormatter::ROUND_HALFDOWN;
const ROUND_HALFEVEN = \NumberFormatter::ROUND_HALFEVEN;
const ROUND_HALFUP = \NumberFormatter::ROUND_HALFUP;
const ROUND_UP = \NumberFormatter::ROUND_UP;
const ROUND_CEILING = \NumberFormatter::ROUND_CEILING;

/**
* {@inheritDoc}
*/
protected function configure()
{
$this->addOption('precision', null);
$this->addOption('grouping', false);
$this->addOption('rounding-mode', self::ROUND_HALFUP);

parent::configure();
}
Expand Down Expand Up @@ -78,6 +87,7 @@ protected function getNumberFormatter()

if ($this->getOption('precision') !== null) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision'));
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->getOption('rounding-mode'));
}

$formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->getOption('grouping'));
Expand Down
Expand Up @@ -43,6 +43,23 @@ public function testTransformWithPrecision()
$this->assertEquals('678,92', $transformer->transform(678.916));
}

public function testTransformWithRoundingMode()
{
$transformer = new NumberToLocalizedStringTransformer(array(
'rounding-mode' => NumberToLocalizedStringTransformer::ROUND_DOWN,
));
$transformer->setLocale('de_AT');
$this->assertEquals('1234,547', $transformer->transform(1234.547), '->transform() only applies rounding mode if precision set');

$transformer = new NumberToLocalizedStringTransformer(array(
'rounding-mode' => NumberToLocalizedStringTransformer::ROUND_DOWN,
'precision' => 2,
));
$transformer->setLocale('de_AT');
$this->assertEquals('1234,54', $transformer->transform(1234.547), '->transform() rounding-mode works');

}

public function testReverseTransform()
{
$transformer = new NumberToLocalizedStringTransformer();
Expand Down

0 comments on commit a305eb0

Please sign in to comment.