Index: branches/5.2.x/core/kernel/utility/formatters/formatter.php =================================================================== --- branches/5.2.x/core/kernel/utility/formatters/formatter.php +++ branches/5.2.x/core/kernel/utility/formatters/formatter.php @@ -180,7 +180,19 @@ $value = sprintf($format, $value); - if ( isset($options['cut_zeros']) && $options['cut_zeros'] ) { + if ( isset($options['keep_cents']) && $options['keep_cents'] ) { + preg_match('/(\d+\.\d{' . $options['keep_cents'] . '})(\d+)?/', $value, $regs); + + // When "keep_cents" option matches format's precision, there will be an "Undefined index: 2" notice. + if ( rtrim($regs[2], '0') === '' ) { + $value = $regs[1]; + } + else { + // Remove trailing zeros in decimal part. + $value = preg_replace('/(\.\d+?)0+$/', '$1', $value); + } + } + elseif ( isset($options['cut_zeros']) && $options['cut_zeros'] ) { // Remove trailing zeros in decimal part (including "." if any left at the end). $value = preg_replace('/\.0+$/', '', $value); $value = preg_replace('/(\.\d+?)0+$/', '$1', $value); Index: branches/5.2.x/core/tests/kernel/utility/formatters/kFormatterTest.php =================================================================== --- branches/5.2.x/core/tests/kernel/utility/formatters/kFormatterTest.php +++ branches/5.2.x/core/tests/kernel/utility/formatters/kFormatterTest.php @@ -0,0 +1,63 @@ +prophesize('kDBItem'); + $item_prophecy->GetFieldOptions('FieldName')->willReturn(array( + 'type' => 'float', + 'format' => '%01.6f', + ) + $field_options); + $item = $item_prophecy->reveal(); + + $this->assertSame($formatted_number, $formatter->Format($raw_number, 'FieldName', $item)); + } + + public function cutZerosDataProvider() + { + return array( + 'long' => array('12.345000', '12.345', array('cut_zeros' => 1)), + 'short' => array('12.300000', '12.3', array('cut_zeros' => 1)), + 'exact+leading_zero' => array('12.030000', '12.03', array('cut_zeros' => 1)), + 'very_long' => array('12.030050', '12.03005', array('cut_zeros' => 1)), + 'no_decimals' => array('12.000000', '12', array('cut_zeros' => 1)), + ); + } + + /** + * @dataProvider keepCentsDataProvider + */ + public function testKeepCents($raw_number, $formatted_number, array $field_options) + { + $formatter = new kFormatter(); + + $item_prophecy = $this->prophesize('kDBItem'); + $item_prophecy->GetFieldOptions('FieldName')->willReturn(array( + 'type' => 'float', + 'format' => '%01.6f', + ) + $field_options); + $item = $item_prophecy->reveal(); + + $this->assertSame($formatted_number, $formatter->Format($raw_number, 'FieldName', $item)); + } + + public function keepCentsDataProvider() + { + return array( + 'long' => array('12.345000', '12.345', array('keep_cents' => 2)), + 'short' => array('12.300000', '12.30', array('keep_cents' => 2)), + 'exact+leading_zero' => array('12.030000', '12.03', array('keep_cents' => 2)), + 'very_long' => array('12.030050', '12.03005', array('keep_cents' => 2)), + 'no_decimals' => array('12.000000', '12.00', array('keep_cents' => 2)), + ); + } + +}