-
Notifications
You must be signed in to change notification settings - Fork 52
/
DecimalToWordsHelper.php
39 lines (36 loc) · 1.34 KB
/
DecimalToWordsHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace App\Helpers;
class DecimalToWordsHelper
{
public static function convertDecimalToWords($number, $currency)
{
$intValue = intval($number);
$hundred = null;
$digitLength = strlen(strval($intValue));
$string = [];
$words = config('constants.amount-to-words');
$digits = ['', 'hundred', 'thousand', 'lakh', 'crore'];
$index = 0;
while ($index < $digitLength) {
$divider = $index == 2 ? 10 : 100;
$number = floor($intValue % $divider);
$intValue = floor($intValue / $divider);
$index += $divider == 10 ? 1 : 2;
if ($number) {
$plural = (($counter = count($string)) && $number > 9) ? 's' : null;
$hundred = $counter == 1 && $string[0] ? ' and ' : null;
$string[] = $number < 21 ? $words[$number] .
' ' . $digits[$counter] . $plural . ' ' . $hundred
:
$words[floor($number / 10) * 10]
. ' ' . $words[$number % 10] . ' '
. $digits[$counter] . $plural . ' ' . $hundred;
} else {
$string[] = null;
}
}
$string = array_reverse($string);
$integerVal = implode('', $string);
return $integerVal . $currency;
}
}