Skip to content

Commit e342e51

Browse files
Merge pull request #8 from nhamtphat/master
Cho phép thay đổi cách đọc phần thập phân
2 parents dcf78f2 + b45669f commit e342e51

File tree

6 files changed

+117
-4
lines changed

6 files changed

+117
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Tất cả lịch sử tiến trình phát triển thư viện
44

5+
# 1.3.0
6+
7+
- Cho phép thay đổi cách đọc phần thập phân.
8+
59
# 1.2.1
610

711
- Hổ trợ PHP8.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ $transformer->toCurrency(95500200);
117117
// tám trăm năm mươi tư triệu chín trăm đồng
118118
$transformer->toCurrency(854000900);
119119

120+
```
121+
Trong một số loại tiền tệ, bạn cần thay đổi cách đọc theo đơn vị quy đổi, ví dụ 1 đô = 100 xen
122+
```php
123+
use PHPViet\NumberToWords\Transformer;
124+
125+
// Đặt số chữ số phần thập phân (tham số đầu tiên Dictionary có thể null)
126+
$transformer = new Transformer(null, 2);
127+
128+
// năm mươi sáu đô chín mươi xen, thay vì năm mươi sáu đô chín xen
129+
$transformer->toCurrency(56.90);
120130
```
121131

122132
Ngoài ra ta còn có thể sử dụng đơn vị tiền tệ khác thông qua tham trị thứ 2 của phương thức

src/Concerns/NumberResolver.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ protected function resolveNumber($number): array
2828
throw new InvalidArgumentException(sprintf('Number arg (`%s`) must be numeric!', $number));
2929
}
3030

31-
$number += 0; // trick xóa các số 0 lẻ sau cùng của phân số đối với input là chuỗi.
32-
$number = (string) $number;
31+
32+
if ($this->decimalPart === null) {
33+
$number += 0; // trick xóa các số 0 lẻ sau cùng của phân số đối với input là chuỗi.
34+
$number = (string) $number;
35+
} else {
36+
$number = number_format($number, $this->decimalPart, '.', '');
37+
}
3338
$minus = '-' === $number[0];
3439

3540
if (false !== strpos($number, '.')) {

src/Transformer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,24 @@ class Transformer
2424
protected $dictionary;
2525

2626
/**
27-
* Tạo đối tượng mới với từ điển chỉ định.
27+
* Mặc định bỏ số các số 0 sau phần thập phân.
28+
* @var int
29+
*/
30+
protected $decimalPart;
31+
32+
/**
33+
* Tạo đối tượng mới với từ điển chỉ định và phần thập phân.
2834
*
2935
* @param DictionaryInterface $dictionary
3036
*/
31-
public function __construct(?DictionaryInterface $dictionary = null)
37+
public function __construct(?DictionaryInterface $dictionary = null, $decimalPart = null)
3238
{
3339
if (null === $dictionary) {
3440
$dictionary = new Dictionary();
3541
}
3642

3743
$this->dictionary = $dictionary;
44+
$this->decimalPart = $decimalPart;
3845
}
3946

4047
/**

tests/CurrencyTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace PHPViet\NumberToWords\Tests;
99

10+
use PHPViet\NumberToWords\Transformer;
11+
1012
/**
1113
* @author Vuong Minh <[email protected]>
1214
* @since 1.0.0
@@ -29,6 +31,61 @@ public function testUSDTransform($expect, $number)
2931
$this->assertEquals($expect, $this->transformer->toCurrency($number, ['đô', 'xen']));
3032
}
3133

34+
/**
35+
* @dataProvider usdDecimalPartDataProvider
36+
*/
37+
public function testUSDSetDecimalPart($expect, $float, $decimalPart)
38+
{
39+
$transformer = new Transformer($this->dictionary, $decimalPart);
40+
41+
$this->assertEquals($expect, $transformer->toCurrency($float, ['đô', 'xen']));
42+
}
43+
44+
public function usdDecimalPartDataProvider(): array
45+
{
46+
return [
47+
['không đô', 0, 1],
48+
['một nghìn đô', 1000, 2],
49+
['một nghìn không trăm linh một đô', 1001, 3],
50+
['một nghìn không trăm linh hai đô', 1002, 4],
51+
['âm không đô mười xen', -0.1, 2],
52+
['âm chín mươi chín đô', -99, 3],
53+
['âm chín mươi tám đô', -98, 4],
54+
['không đô ba trăm bảy mươi chín xen', 0.378758, 3],
55+
['không đô chín mươi hai xen', 0.922174, 2],
56+
['năm trăm bảy mươi ba đô năm mươi tám xen', 573.58, 2],
57+
['sáu trăm sáu mươi chín đô mười bốn xen', 669.135, 2],
58+
['ba trăm chín mươi lăm đô mười bốn xen', 395.136, 2],
59+
];
60+
}
61+
62+
/**
63+
* @dataProvider decimalPartDataProvider
64+
*/
65+
public function testSetDecimalPart($expect, $float, $decimalPart)
66+
{
67+
$transformer = new Transformer($this->dictionary, $decimalPart);
68+
$this->assertEquals($expect, $transformer->toCurrency($float));
69+
}
70+
71+
public function decimalPartDataProvider(): array
72+
{
73+
return [
74+
['không đồng', 0, 1],
75+
['một nghìn đồng', 1000, 2],
76+
['một nghìn không trăm linh một đồng', 1001, 3],
77+
['một nghìn không trăm linh hai đồng', 1002, 4],
78+
['âm không phẩy mười đồng', -0.1, 2],
79+
['âm chín mươi chín đồng', -99, 3],
80+
['âm chín mươi tám đồng', -98, 4],
81+
['không phẩy ba trăm bảy mươi chín đồng', 0.378758, 3],
82+
['không phẩy chín mươi hai đồng', 0.922174, 2],
83+
['năm trăm bảy mươi ba phẩy năm mươi tám đồng', 573.58, 2],
84+
['sáu trăm sáu mươi chín phẩy mười bốn đồng', 669.135, 2],
85+
['ba trăm chín mươi lăm phẩy mười bốn đồng', 395.136, 2],
86+
];
87+
}
88+
3289
public function usdDataProvider(): array
3390
{
3491
return [

tests/NumberTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace PHPViet\NumberToWords\Tests;
99

10+
use PHPViet\NumberToWords\Transformer;
11+
1012
/**
1113
* @author Vuong Minh <[email protected]>
1214
* @since 1.0.0
@@ -37,6 +39,34 @@ public function testFractionTransform($expect, $float)
3739
$this->assertEquals($expect, $this->transformer->toWords($float));
3840
}
3941

42+
/**
43+
* @dataProvider decimalPartDataProvider
44+
*/
45+
public function testSetDecimalPart($expect, $float, $decimalPart)
46+
{
47+
$transformer = new Transformer($this->dictionary, $decimalPart);
48+
49+
$this->assertEquals($expect, $transformer->toWords($float));
50+
}
51+
52+
public function decimalPartDataProvider(): array
53+
{
54+
return [
55+
['không', 0, 1],
56+
['một nghìn', 1000, 2],
57+
['một nghìn không trăm linh một', 1001, 3],
58+
['một nghìn không trăm linh hai', 1002, 4],
59+
['âm không phẩy mười', -0.1, 2],
60+
['âm chín mươi chín', -99, 3],
61+
['âm chín mươi tám', -98, 4],
62+
['không phẩy ba trăm bảy mươi chín', 0.378758, 3],
63+
['không phẩy chín mươi hai', 0.922174, 2],
64+
['năm trăm bảy mươi ba phẩy năm mươi tám', 573.58, 2],
65+
['sáu trăm sáu mươi chín phẩy mười bốn', 669.135, 2],
66+
['ba trăm chín mươi lăm phẩy mười bốn', 395.136, 2],
67+
];
68+
}
69+
4070
public function fractionDataProvider(): array
4171
{
4272
return [

0 commit comments

Comments
 (0)