Skip to content

Commit 96cd537

Browse files
committed
Initial commit of php-terbilang
0 parents  commit 96cd537

11 files changed

+547
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
/.idea/
3+
/build/
4+
/composer.lock
5+
*.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Nur Muhammad
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# PHP Terbilang
2+
3+
PHP Terbilang is a library that converts number or currency amount into their Indonesian words representation.
4+
5+
## Features
6+
7+
- Converts number values to words in Indonesian.
8+
- Converts currency amounts (in rupiah) to words.
9+
- Supported decimals (comma) for both regular number and currency.
10+
11+
## Installation
12+
13+
You can install via Composer. Run the following command:
14+
15+
```sh
16+
composer require ngekoding/terbilang
17+
```
18+
19+
## Usage
20+
21+
### Basic Usage
22+
23+
```php
24+
use Ngekoding\Terbilang\Terbilang;
25+
26+
// Convert a regular number to words
27+
echo Terbilang::convert(12345); // Outputs: "dua belas ribu tiga ratus empat puluh lima"
28+
29+
echo Terbilang::convert(12345.67); // Outputs: "dua belas ribu tiga ratus empat puluh lima koma enam tujuh"
30+
31+
// Convert a currency amount to words
32+
echo Terbilang::convert(12345, true); // Outputs: "dua belas ribu tiga ratus empat puluh lima rupiah"
33+
34+
echo Terbilang::convert(12345.67, true); // Outputs: "dua belas ribu tiga ratus empat puluh lima rupiah enam puluh tujuh sen"
35+
```
36+
37+
### Options
38+
39+
You can customize the decimal separator for your conversions:
40+
41+
```php
42+
// Convert with a custom decimal separator (e.g., ',')
43+
echo Terbilang::convert('12345,67', true, ','); // Outputs: "dua belas ribu tiga ratus empat puluh lima rupiah enam puluh tujuh sen"
44+
45+
// Or you can change it globally
46+
Terbilang::setDecimalSeparator(',');
47+
48+
echo Terbilang::convert('12345,67'); // Outputs: "dua belas ribu tiga ratus empat puluh lima koma enam tujuh"
49+
```
50+
51+
**Note:** Use string input when you working with a decimals numbers and want get exactly as is the input value.
52+
53+
## API
54+
55+
```php
56+
Terbilang::convert($number, $isCurrency = false, $decimalSeparator = null)
57+
```
58+
59+
Converts a number or currency amount into its Indonesian words representation.
60+
61+
- **$number** (mixed): The number or currency amount to convert.
62+
- **$isCurrency** (bool, optional): Whether the conversion is for currency (default: false).
63+
- **$decimalSeparator** (string, optional): The decimal separator used in the number (default: '.').
64+
65+
## References
66+
67+
This library aims to adhere to Indonesian language standards.
68+
69+
- [Pedoman Umum Ejaan Bahasa Indonesia (PUEBI)](https://badanbahasa.kemdikbud.go.id/lamanbahasa/sites/default/files/PUEBI.pdf)
70+
- [Kamus Besar Bahasa Indonesia (KBBI) Kemdikbud](https://kbbi.kemdikbud.go.id)
71+
72+
## Contributing
73+
74+
Feel free to contribute to improve this library. Fork it, make changes, and submit a pull request.
75+
76+
## License
77+
78+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "ngekoding/terbilang",
3+
"description": "A library to convert numbers and currency to words in Indonesian.",
4+
"type": "library",
5+
"keywords": [
6+
"terbilang",
7+
"numbers",
8+
"to",
9+
"words",
10+
"currency",
11+
"money"
12+
],
13+
"license": "MIT",
14+
"autoload": {
15+
"psr-4": {
16+
"Ngekoding\\Terbilang\\": "src/"
17+
}
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"Ngekoding\\Terbilang\\": "tests/"
22+
}
23+
},
24+
"authors": [
25+
{
26+
"name": "Nur Muhammad",
27+
"email": "blog.nurmuhammad@gmail.com"
28+
}
29+
],
30+
"require": {
31+
"php": ">=5.6"
32+
},
33+
"require-dev": {
34+
"phpunit/phpunit": "*"
35+
},
36+
"scripts": {
37+
"test": "./vendor/bin/phpunit"
38+
}
39+
}

phpunit.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit>
3+
<testsuites>
4+
<testsuite name="default">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

src/Exception/NotNumberException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Ngekoding\Terbilang\Exception;
4+
5+
class NotNumberException extends \Exception
6+
{
7+
}

src/Helper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ngekoding\Terbilang;
4+
5+
class Helper
6+
{
7+
/**
8+
* Removes non-numeric characters from a string and returns the result as an integer.
9+
*
10+
* @param string $input The input string to be cleaned.
11+
* @return int The cleaned string converted to an integer.
12+
*/
13+
public static function digitsOnly($str)
14+
{
15+
return intval(preg_replace('/\D/', '', $str));
16+
}
17+
}

src/NumberToWordsConverter.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Ngekoding\Terbilang;
4+
5+
use Ngekoding\Terbilang\Exception\NotNumberException;
6+
7+
class NumberToWordsConverter
8+
{
9+
/**
10+
* Array mapping digits to their Indonesian word equivalents.
11+
* @var array
12+
*/
13+
public static $digits = [
14+
'',
15+
'satu',
16+
'dua',
17+
'tiga',
18+
'empat',
19+
'lima',
20+
'enam',
21+
'tujuh',
22+
'delapan',
23+
'sembilan',
24+
'sepuluh',
25+
'sebelas'
26+
];
27+
28+
/**
29+
* Array defining the bases and their corresponding Indonesian word units.
30+
* @var array
31+
*/
32+
public static $bases = [
33+
1000000000000000000 => 'kuintiliun',
34+
1000000000000000 => 'kuadriliun',
35+
1000000000000 => 'triliun',
36+
1000000000 => 'miliar',
37+
1000000 => 'juta',
38+
1000 => 'ribu',
39+
100 => 'ratus',
40+
];
41+
42+
/**
43+
* Convert a numeric value into its Indonesian words representation.
44+
*
45+
* @param int $number The number to convert.
46+
* @return string The Indonesian words representation of the number.
47+
* @throws NotNumberException If the input is not a valid number.
48+
*/
49+
public static function convert($number)
50+
{
51+
if ( ! is_numeric($number)) {
52+
throw new NotNumberException('The input must be a number.');
53+
}
54+
55+
if ($number == 0) {
56+
return 'nol';
57+
}
58+
59+
$result = '';
60+
61+
if ($number < 0) {
62+
$number = abs($number);
63+
$result = 'minus ';
64+
}
65+
66+
foreach (self::$bases as $base => $unit) {
67+
if ($number >= $base) {
68+
$remaining = floor($number / $base);
69+
$number %= $base;
70+
71+
if ($remaining == 1) {
72+
$result .= ' ' . ($base <= 1000 ? 'se' : 'satu ') . $unit;
73+
} else {
74+
$result .= ' ' . self::convert($remaining) . ' ' . $unit;
75+
}
76+
}
77+
}
78+
79+
if ($number < 12) {
80+
$result .= ' ' . self::$digits[$number];
81+
} elseif ($number < 20) {
82+
$result .= ' ' . self::$digits[$number - 10] . ' belas';
83+
} else {
84+
$remaining = floor($number / 10);
85+
$number %= 10;
86+
$result .= ' ' . self::$digits[$remaining] . ' puluh ' . ($number > 0 ? self::convert($number) : '');
87+
}
88+
89+
return trim(preg_replace('/\s+/', ' ', $result));
90+
}
91+
}

0 commit comments

Comments
 (0)