[Form] added support for rounding-mode option on NumberFields
This commit is contained in:
parent
2052501634
commit
a305eb063d
@ -27,10 +27,12 @@ class NumberField extends InputField
|
||||
// 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'),
|
||||
)));
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,14 @@ use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
|
||||
*/
|
||||
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}
|
||||
*/
|
||||
@ -20,6 +28,7 @@ class NumberToLocalizedStringTransformer extends BaseValueTransformer
|
||||
{
|
||||
$this->addOption('precision', null);
|
||||
$this->addOption('grouping', false);
|
||||
$this->addOption('rounding-mode', self::ROUND_HALFUP);
|
||||
|
||||
parent::configure();
|
||||
}
|
||||
@ -78,6 +87,7 @@ class NumberToLocalizedStringTransformer extends BaseValueTransformer
|
||||
|
||||
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'));
|
||||
|
@ -43,6 +43,23 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
$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();
|
||||
|
Reference in New Issue
Block a user