├── .gitignore ├── LICENSE.md ├── composer.json ├── phpunit.xml ├── readme.md ├── src ├── Converter.php ├── Dictionary.php └── PersianNumberToWords.php └── tests ├── BaseTest.php └── TestArray.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | composer.lock 4 | /web-test -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | #The MIT License 2 | 3 | Copyright (c) Mojtabaa HN 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. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mojtabaahn/php-persian-number-to-words", 3 | "description": "Converts number to words in persian", 4 | "keywords": [ 5 | "numbers-to-words", 6 | "persian" 7 | ], 8 | "homepage": "https://github.com/mojtabaahn/php-number-to-words", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "mojtabaa hn", 13 | "email": "mojtabaa.hn@gmail.com", 14 | "role": "Developer" 15 | } 16 | ], 17 | "require": { 18 | "php": "^7.4|^8.0" 19 | }, 20 | "require-dev": { 21 | "pestphp/pest": "^0.2.3|^1.0", 22 | "phpunit/phpunit": "^9.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "MojtabaaHN\\PersianNumberToWords\\": "./src" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "MojtabaaHN\\PersianNumberToWords\\Tests\\": "./tests" 32 | } 33 | }, 34 | "scripts": { 35 | "test": "pest" 36 | }, 37 | "minimum-stability": "dev", 38 | "prefer-stable": true 39 | } 40 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | ./tests 10 | 11 | 12 | 13 | 14 | ./app 15 | ./src 16 | 17 | 18 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Convert Numbers to Words In Persian 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/mojtabaahn/php-persian-number-to-words.svg?style=flat-square)](https://packagist.org/packages/mojtabaahn/php-persian-number-to-words) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/mojtabaahn/php-persian-number-to-words?style=flat-square)](https://packagist.org/packages/mojtabaahn/php-persian-number-to-words) 5 | [![Repo Size](https://img.shields.io/github/repo-size/mojtabaahn/php-persian-number-to-words?style=flat-square)](https://packagist.org/packages/mojtabaahn/php-persian-number-to-words) 6 | [![Repo Size](https://img.shields.io/packagist/l/mojtabaahn/php-persian-number-to-words?style=flat-square)](https://packagist.org/packages/mojtabaahn/php-persian-number-to-words) 7 | 8 | This packages offers ability to convert numbers to words in persian. Words and phrases are fully configurable, so it is possible to use it for all persian language family with configuration. 9 | 10 | ## Requirement 11 | 12 | This package requires **PHP 7.4** or higher. 13 | 14 | ## Installation 15 | 16 | You can install the package via composer: 17 | 18 | ```bash 19 | composer require mojtabaahn/php-persian-number-to-words 20 | ``` 21 | 22 | ## Usage 23 | 24 | ``` php 25 | $dictionary = new MojtabaaHN\PersianNumberToWords\Dictionary(); 26 | 27 | $converter = new MojtabaaHN\PersianNumberToWords\PersianNumberToWords($dictionary); 28 | 29 | echo $converter->convert(0); 30 | // صفر 31 | 32 | echo $converter->convert(-10); 33 | // منفی ده 34 | 35 | echo $converter->convert(229); 36 | // دویست و بیست و نه 37 | 38 | echo $converter->convert(999999999); 39 | // نهصد و نود و نه میلیون و نهصد و نود و نه هزار و نهصد و نود و نه 40 | ``` 41 | ## Configuration 42 | 43 | It is possible to customize the way output should look like using Dictionary class setter methods. 44 | 45 | ``` php 46 | $dictionary = (new MojtabaaHN\PersianNumberToWords\Dictionary()) 47 | ->setZero('هیچ') 48 | ->setNegative('منهای') 49 | ->setSeparator(' ُ '); 50 | 51 | // Also ->setUnits(array $units) & -> setSuffixes(array $suffixes) are availabe 52 | 53 | $converter = new MojtabaaHN\PersianNumberToWords\PersianNumberToWords($dictionary); 54 | 55 | echo $converter->convert(0); 56 | // هیچ 57 | 58 | echo $converter->convert(-10); 59 | // منهای ده 60 | 61 | echo $converter->convert(229); 62 | // دویست ُ بیست ُ نه 63 | 64 | ``` 65 | 66 | ## Default Configuration 67 | This code is a part of Dictionary class, you can see all default Configuration: 68 | ```php 69 | class Dictionary 70 | { 71 | 72 | protected string $zero = 'صفر'; 73 | 74 | protected string $negative = 'منفی'; 75 | 76 | protected string $separator = " و "; 77 | 78 | protected array $units = [ 79 | 1 => 'یک', 80 | 2 => 'دو', 81 | 3 => 'سه', 82 | 4 => 'چهار', 83 | 5 => 'پنج', 84 | 6 => 'شش', 85 | 7 => 'هفت', 86 | 8 => 'هشت', 87 | 9 => 'نه', 88 | 10 => 'ده', 89 | 11 => 'یازده', 90 | 12 => 'دوازده', 91 | 13 => 'سیزده', 92 | 14 => 'چهارده', 93 | 15 => 'پانزده', 94 | 16 => 'شانزده', 95 | 17 => 'هفده', 96 | 18 => 'هجده', 97 | 19 => 'نوزده', 98 | 20 => 'بیست', 99 | 30 => 'سی', 100 | 40 => 'چهل', 101 | 50 => 'پنجاه', 102 | 60 => 'شصت', 103 | 70 => 'هفتاد', 104 | 80 => 'هشتاد', 105 | 90 => 'نود', 106 | 100 => 'صد', 107 | 200 => 'دویست', 108 | 300 => 'سیصد', 109 | 400 => 'چهارصد', 110 | 500 => 'پانصد', 111 | 600 => 'ششصد', 112 | 700 => 'هفتصد', 113 | 800 => 'هشتصد', 114 | 900 => 'نهصد' 115 | ]; 116 | 117 | protected array $suffixes = [ 118 | 3 => 'هزار', 119 | 6 => 'میلیون', 120 | 9 => 'میلیارد', 121 | 12 => 'بیلیون', 122 | 15 => 'بیلیارد', 123 | 18 => 'تریلیون', 124 | 21 => 'تریلیارد', 125 | 24 => 'کوآدریلیون', 126 | 27 => 'کادریلیارد', 127 | 30 => 'کوینتیلیون', 128 | 33 => 'کوانتینیارد', 129 | 36 => 'سکستیلیون', 130 | 39 => 'سکستیلیارد', 131 | 42 => 'سپتیلیون', 132 | 45 => 'سپتیلیارد', 133 | 48 => 'اکتیلیون', 134 | 51 => 'اکتیلیارد', 135 | 54 => 'نانیلیون', 136 | 57 => 'نانیلیارد', 137 | 60 => 'دسیلیون', 138 | 63 => 'دسیلیارد', 139 | ]; 140 | 141 | // Setters & Helpers... 142 | 143 | } 144 | ``` 145 | 146 | ## Testing 147 | 148 | ``` bash 149 | composer test 150 | ``` 151 | 152 | ## License 153 | 154 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. -------------------------------------------------------------------------------- /src/Converter.php: -------------------------------------------------------------------------------- 1 | dictionary = $dictionary; 14 | } 15 | 16 | protected bool $isNegative = false; 17 | 18 | public function convert(int $number, bool $initial = true) 19 | { 20 | if ($number === 0 && $initial) { 21 | return $this->dictionary->zero(); 22 | } 23 | 24 | $number = $this->setNegativeFlagAndGetAbsolute($number); 25 | 26 | list($chunk, $chunkLevel, $remainder) = $this->splitLargestChunk($number); 27 | 28 | $convertedRemainder = $this->convertRemainder($remainder); 29 | 30 | $convertedTriplet = $this->convertTriplet($chunk, $chunkLevel); 31 | 32 | $convertedNumber = $this->dictionary->separate($convertedTriplet, $convertedRemainder); 33 | 34 | return $this->applySign($convertedNumber, $initial); 35 | } 36 | 37 | protected function setNegativeFlagAndGetAbsolute(int $number) 38 | { 39 | if ($number < 0) { 40 | $this->isNegative = true; 41 | } 42 | return abs($number); 43 | } 44 | 45 | protected function splitLargestChunk(int $number) 46 | { 47 | $stringNumber = (string)$number; 48 | $length = strlen($stringNumber); 49 | 50 | $chunkIndex = $length % 3; 51 | $chunkIndex = $chunkIndex !== 0 ? $chunkIndex : 3; 52 | 53 | $chunkLevel = $length - $chunkIndex; 54 | $chunk = substr($stringNumber, 0, $chunkIndex); 55 | $remainder = substr($stringNumber, $chunkIndex); 56 | 57 | return [$chunk, $chunkLevel, $remainder]; 58 | } 59 | 60 | public function convertRemainder(string $remainder): string 61 | { 62 | if ($remainder === '') { 63 | return ''; 64 | } 65 | 66 | return $this->convert((int)$remainder, false); 67 | } 68 | 69 | protected function applySign(string $convertedNumber, bool $isInitial): string 70 | { 71 | if ($this->isNegative && $isInitial) { 72 | return $this->dictionary->addNegative($convertedNumber); 73 | } 74 | 75 | return $convertedNumber; 76 | } 77 | 78 | protected function convertTriplet(string $chunk, $chunkLevel) 79 | { 80 | $triplet = (int)$chunk; 81 | 82 | $units = $triplet % 10; 83 | $tens = (int)($triplet / 10) % 10; 84 | $hundreds = (int)($triplet / 100) % 10; 85 | 86 | $result = []; 87 | if ($hundreds !== 0) { 88 | $result[] = $this->dictionary->unit($hundreds * 100); 89 | } 90 | 91 | if ($tens === 1) { 92 | $result[] = $this->dictionary->unit(10 + $units); 93 | } else { 94 | if ($tens !== 0) { 95 | $result[] = $this->dictionary->unit($tens * 10); 96 | } 97 | if ($units !== 0) { 98 | $result[] = $this->dictionary->unit($units); 99 | } 100 | } 101 | 102 | $result = $this->dictionary->separate(...$result); 103 | 104 | if ($chunkLevel !== 0) { 105 | $result = $this->dictionary->addSuffix($result, $chunkLevel); 106 | } 107 | 108 | return $result; 109 | } 110 | } -------------------------------------------------------------------------------- /src/Dictionary.php: -------------------------------------------------------------------------------- 1 | 'یک', 16 | 2 => 'دو', 17 | 3 => 'سه', 18 | 4 => 'چهار', 19 | 5 => 'پنج', 20 | 6 => 'شش', 21 | 7 => 'هفت', 22 | 8 => 'هشت', 23 | 9 => 'نه', 24 | 10 => 'ده', 25 | 11 => 'یازده', 26 | 12 => 'دوازده', 27 | 13 => 'سیزده', 28 | 14 => 'چهارده', 29 | 15 => 'پانزده', 30 | 16 => 'شانزده', 31 | 17 => 'هفده', 32 | 18 => 'هجده', 33 | 19 => 'نوزده', 34 | 20 => 'بیست', 35 | 30 => 'سی', 36 | 40 => 'چهل', 37 | 50 => 'پنجاه', 38 | 60 => 'شصت', 39 | 70 => 'هفتاد', 40 | 80 => 'هشتاد', 41 | 90 => 'نود', 42 | 100 => 'صد', 43 | 200 => 'دویست', 44 | 300 => 'سیصد', 45 | 400 => 'چهارصد', 46 | 500 => 'پانصد', 47 | 600 => 'ششصد', 48 | 700 => 'هفتصد', 49 | 800 => 'هشتصد', 50 | 900 => 'نهصد' 51 | ]; 52 | 53 | protected array $suffixes = [ 54 | 3 => 'هزار', 55 | 6 => 'میلیون', 56 | 9 => 'میلیارد', 57 | 12 => 'بیلیون', 58 | 15 => 'بیلیارد', 59 | 18 => 'تریلیون', 60 | 21 => 'تریلیارد', 61 | 24 => 'کوآدریلیون', 62 | 27 => 'کادریلیارد', 63 | 30 => 'کوینتیلیون', 64 | 33 => 'کوانتینیارد', 65 | 36 => 'سکستیلیون', 66 | 39 => 'سکستیلیارد', 67 | 42 => 'سپتیلیون', 68 | 45 => 'سپتیلیارد', 69 | 48 => 'اکتیلیون', 70 | 51 => 'اکتیلیارد', 71 | 54 => 'نانیلیون', 72 | 57 => 'نانیلیارد', 73 | 60 => 'دسیلیون', 74 | 63 => 'دسیلیارد', 75 | ]; 76 | 77 | public function setZero(string $zero): Dictionary 78 | { 79 | $this->zero = $zero; 80 | return $this; 81 | } 82 | 83 | public function setNegative(string $negative): Dictionary 84 | { 85 | $this->negative = $negative; 86 | return $this; 87 | } 88 | 89 | public function setSeparator(string $separator): Dictionary 90 | { 91 | $this->separator = $separator; 92 | return $this; 93 | } 94 | 95 | public function setUnits(array $units): Dictionary 96 | { 97 | $this->units = $units; 98 | return $this; 99 | } 100 | 101 | public function setSuffixes($suffixes): Dictionary 102 | { 103 | $this->suffixes = $suffixes; 104 | return $this; 105 | } 106 | 107 | public function zero(): string 108 | { 109 | return $this->zero; 110 | } 111 | 112 | public function addNegative(string $arg) 113 | { 114 | return $this->negative . ' ' . $arg; 115 | } 116 | 117 | public function separate(string ...$args) 118 | { 119 | $filteredArgs = array_filter($args, fn($input) => $input !== ''); 120 | return implode($this->separator, $filteredArgs); 121 | } 122 | 123 | public function unit(int $unit) 124 | { 125 | return $this->units[$unit]; 126 | } 127 | 128 | public function addSuffix(string $string, int $level) 129 | { 130 | return $string . ' ' . $this->suffixes[$level]; 131 | } 132 | 133 | } -------------------------------------------------------------------------------- /src/PersianNumberToWords.php: -------------------------------------------------------------------------------- 1 | dictionary = $dictionary; 14 | } 15 | 16 | public function convert(int $number) 17 | { 18 | return (new Converter($this->dictionary))->convert($number); 19 | } 20 | } -------------------------------------------------------------------------------- /tests/BaseTest.php: -------------------------------------------------------------------------------- 1 | $testItem) { 11 | it("assert converts {$testItem[0]} to string with no errors") 12 | ->assertEquals($testItem[1], $instance->convert($testItem[0])); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /tests/TestArray.php: -------------------------------------------------------------------------------- 1 |