Skip to content

Commit

Permalink
Support for decimal numbers added
Browse files Browse the repository at this point in the history
  • Loading branch information
esmatullaharifi committed Oct 15, 2021
1 parent 2d20e18 commit 7d709c5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ use Arifi\NumbersToPersianConverter;
$string = new NumbersToPersianConverter();
$number = 123;
echo $string->convert($number); // یک‌صد و بيست و سه

$number = 123.43;
echo $string->convert($number); // يك‌صد و بيست و سه عشاریه چهار سه
```
9 changes: 9 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

include('../src/NumbersToPersianConverter.php');

use Arifi\NumbersToPersianConverter;

$string = new NumbersToPersianConverter();
$number = 123.43;
echo $string->convert($number);
27 changes: 24 additions & 3 deletions src/NumbersToPersianConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ private function thirteenDigits($number)
return $words;
}

public function convert($number)
{
private function convertParts ($number) {
if ($number >= 1 && $number <= 9) {
$words = $this->oneDigit($number);
} elseif ($number >= 10 && $number <= 99) {
Expand All @@ -203,4 +202,26 @@ public function convert($number)
}
return $words;
}
}

/**
* Convert numeric input into words
*
* @param $number 13 digits number only
* @return string
*/
public function convert($number)
{
$numberParts = explode('.', $number);

$words = $this->convertParts($numberParts[0]);
if (isset($numberParts[1])) {
$words .= ' عشاریه ';
$numberArray = str_split($numberParts[1]);
foreach ($numberArray as $num) {
$words .= $this->oneDigit($num) . (next($numberArray) ? ' ' : '');
}
}

return $words;
}
}

0 comments on commit 7d709c5

Please sign in to comment.