Created
July 2, 2025 11:56
-
-
Save alalfakawma/65fa210390e78c45728d57aa0e6159f2 to your computer and use it in GitHub Desktop.
Amount to words (Indian Currency)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function amountToWords($number) { | |
$no = floor($number); | |
$decimal = round(($number - $no) * 100); | |
$words = array( | |
0 => '', 1 => 'One', 2 => 'Two', 3 => 'Three', | |
4 => 'Four', 5 => 'Five', 6 => 'Six', 7 => 'Seven', | |
8 => 'Eight', 9 => 'Nine', 10 => 'Ten', 11 => 'Eleven', | |
12 => 'Twelve', 13 => 'Thirteen', 14 => 'Fourteen', | |
15 => 'Fifteen', 16 => 'Sixteen', 17 => 'Seventeen', | |
18 => 'Eighteen', 19 => 'Nineteen', 20 => 'Twenty', | |
30 => 'Thirty', 40 => 'Forty', 50 => 'Fifty', | |
60 => 'Sixty', 70 => 'Seventy', 80 => 'Eighty', 90 => 'Ninety' | |
); | |
$digits = array('', 'Thousand', 'Lakh', 'Crore'); | |
$str = array(); | |
// Safely convert any number <= 999 into words | |
function convert_hundred($num, $words) { | |
$output = ''; | |
if ($num > 99) { | |
$output .= $words[floor($num / 100)] . ' Hundred'; | |
$num = $num % 100; | |
if ($num > 0) $output .= ' and '; | |
} | |
if ($num > 0) { | |
if ($num < 21) { | |
$output .= $words[$num]; | |
} else { | |
$output .= $words[floor($num / 10) * 10]; | |
if ($num % 10 > 0) { | |
$output .= ' ' . $words[$num % 10]; | |
} | |
} | |
} | |
return $output; | |
} | |
// Indian grouping: last 3 digits, then 2 by 2 | |
$levels = array(); | |
$levels[] = $no % 1000; // Last 3 digits | |
$no = floor($no / 1000); | |
while ($no > 0) { | |
$levels[] = $no % 100; | |
$no = floor($no / 100); | |
} | |
foreach ($levels as $i => $part) { | |
if ($part == 0) continue; | |
$label = $digits[$i] ?? ''; | |
$str[] = convert_hundred($part, $words) . ($label ? ' ' . $label : ''); | |
} | |
$rupees = implode(' ', array_reverse($str)); | |
$paise = ''; | |
if ($decimal > 0) { | |
$paise = convert_hundred($decimal, $words) . ' Paise'; | |
$paise = ' and ' . $paise; | |
} | |
return 'Rupees ' . trim($rupees) . $paise . ' Only'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment