Created
December 20, 2021 10:19
-
-
Save HazzazBinFaiz/1b1d328f74304ded4d4cf86efe3271d8 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class NumberToString | |
{ | |
public static function convertNumber($number) | |
{ | |
$crore = floor($number / 100000000); | |
$number -= $crore * 100000000; | |
$lakh = floor($number / 1000000); | |
$number -= $lakh * 100000; | |
$thousand = floor($number / 1000); | |
$number -= $thousand * 1000; | |
$hundred = floor($number / 100); | |
$number -= $hundred * 100; | |
$deca = floor($number / 10); | |
$n = $number % 10; | |
$result = ""; | |
if ($crore) { | |
$result .= static::convertNumber($crore) . " Crore"; | |
} | |
if ($lakh) { | |
$result .= (empty($result) ? "" : " ") . static::convertNumber($thousand) . " Lakh"; | |
} | |
if ($thousand) { | |
$result .= (empty($result) ? "" : " ") . static::convertNumber($thousand) . " Thousand"; | |
} | |
if ($hundred) { | |
$result .= (empty($result) ? "" : " ") . static::convertNumber($hundred) . " Hundred"; | |
} | |
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen", "Nineteen"); | |
$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eigthy", "Ninety"); | |
if ($deca || $n) { | |
if (!empty($result)) { | |
$result .= " and "; | |
} | |
if ($deca < 2) { | |
$result .= $ones[$deca * 10 + $n]; | |
} else { | |
$result .= $tens[$deca]; | |
if ($n) { | |
$result .= "-" . $ones[$n]; | |
} | |
} | |
} | |
if (empty($result)) { | |
$result = "zero"; | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment