diff --git a/composer.json b/composer.json index 43cc1f8..945de17 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,11 @@ "name": "Tomáš Prokop", "homepage": "https://github.com/naril", "role": "Developer" + }, + { + "name": "Király Bálint", + "homepage": "https://github.com/kiralybalint", + "role": "Developer" } ], "minimum-stability": "stable", diff --git a/src/field/NumericField.php b/src/field/NumericField.php index de679d3..3acf1f3 100644 --- a/src/field/NumericField.php +++ b/src/field/NumericField.php @@ -30,10 +30,10 @@ public function __construct() { */ public function toData($value) { if (is_null($value)) { - return ""; + return sprintf('%'. $this->getLength().'s', ''); } else { $value = number_format((float) $value, $this->getDecimalCount(), '.', ''); - return substr(strval($value), 0, $this->getLength()); + return str_pad(substr(strval($value), 0, $this->getLength()), $this->length, ' ', STR_PAD_LEFT); } } diff --git a/tests/unit/field/NumericFieldTest.php b/tests/unit/field/NumericFieldTest.php index 5718129..4b019c3 100644 --- a/tests/unit/field/NumericFieldTest.php +++ b/tests/unit/field/NumericFieldTest.php @@ -38,7 +38,8 @@ public function dataFromData() { array('1234', 123), array('123', 123), array(123, 123), - array(' 2', 2), + array(' 2', 2), + array('2', 2), array(null, 0), ); } @@ -49,10 +50,10 @@ public function dataFromData() { public function dataToData() { return array( array(123456, '123'), - array(1, '1'), - array(null, ''), - array(false, '0'), - array('', '0'), + array(1, ' 1'), + array(null, ' '), + array(false, ' 0'), + array('', ' 0'), ); }