Skip to content

Commit 8e39e25

Browse files
committed
fix #99
1 parent ec98ad4 commit 8e39e25

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/XBase/DataConverter/Field/DBase/NumberConverter.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public function toBinaryString($value): string
3434
return str_repeat(chr(0x00), $this->column->getLength());
3535
}
3636

37-
if ($value % 1 > 0) {
38-
$value = number_format($value, $this->column->getDecimalCount(), '.', '');
39-
}
40-
41-
return str_pad((string) $value, $this->column->getLength(), ' ', STR_PAD_LEFT);
37+
return str_pad(
38+
number_format($value, $this->column->getDecimalCount(), '.', ''),
39+
$this->column->getLength(),
40+
' ',
41+
STR_PAD_LEFT
42+
);
4243
}
4344
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace XBase\Tests\DataConverter\Field\DBase;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use XBase\Column\ColumnInterface;
7+
use XBase\DataConverter\Field\DBase\NumberConverter;
8+
use XBase\Table;
9+
10+
/**
11+
* @author Alexander Strizhak <[email protected]>
12+
*
13+
* @coversDefaultClass \XBase\DataConverter\Field\DBase\NumberConverter
14+
*/
15+
class NumberConverterTest extends TestCase
16+
{
17+
/**
18+
* Issue #99
19+
* @covers ::toBinaryString
20+
* @dataProvider dataRightDecimalCount
21+
*
22+
* @param int|float $in
23+
*/
24+
public function testRightDecimalCount(int $length, int $decimalCount, $in, string $out)
25+
{
26+
$table = $this->createMock(Table::class);
27+
$column = $this->createMock(ColumnInterface::class);
28+
$column
29+
->method('getLength')
30+
->willReturn($length);
31+
$column
32+
->method('getDecimalCount')
33+
->willReturn($decimalCount);
34+
35+
$fieldConverter = new NumberConverter($table, $column);
36+
self::assertSame($out, $fieldConverter->toBinaryString($in));
37+
}
38+
39+
public function dataRightDecimalCount()
40+
{
41+
yield [10, 3, null, str_repeat(chr(0x00), 10)];
42+
yield [10, 0, 10, ' 10'];
43+
yield [10, 3, 10.123, ' 10.123'];
44+
yield [10, 3, 10, ' 10.000'];
45+
yield [10, 3, 1, ' 1.000'];
46+
}
47+
}

0 commit comments

Comments
 (0)