Created
June 25, 2015 11:11
-
-
Save efenacigiray/d87c960201ed17ecd717 to your computer and use it in GitHub Desktop.
Rakam > Yazı Çevirici Türk Lirası
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
function convert_number_to_words($number) { | |
$hyphen = '-'; | |
$conjunction = ' '; | |
$separator = ' '; | |
$negative = 'eksi '; | |
$big_currency = ' türk lirası '; | |
$sma_currency = ' kuruş'; | |
$dictionary = array( | |
0 => 'sıfır', | |
1 => 'bir', | |
2 => 'iki', | |
3 => 'üç', | |
4 => 'dört', | |
5 => 'beş', | |
6 => 'altı', | |
7 => 'yedi', | |
8 => 'sekiz', | |
9 => 'dokuz', | |
10 => 'on', | |
11 => 'onbir', | |
12 => 'oniki', | |
13 => 'onüç', | |
14 => 'ondört', | |
15 => 'onbeş', | |
16 => 'onaltı', | |
17 => 'onyedi', | |
18 => 'onsekiz', | |
19 => 'ondokuz', | |
20 => 'yirmi', | |
30 => 'otuz', | |
40 => 'kırk', | |
50 => 'elli', | |
60 => 'atmış', | |
70 => 'yetmiş', | |
80 => 'seksen', | |
90 => 'doksan', | |
100 => 'yüz', | |
1000 => 'bin', | |
1000000 => 'milyon', | |
1000000000 => 'milyar', | |
1000000000000 => 'trilyon', | |
1000000000000000 => 'quadrilyon', | |
1000000000000000000 => 'quintillion' | |
); | |
if (!is_numeric($number)) { | |
return false; | |
} | |
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) { | |
trigger_error( | |
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, | |
E_USER_WARNING | |
); | |
return false; | |
} | |
if ($number < 0) { | |
return $negative . convert_number_to_words(abs($number)); | |
} | |
$string = $fraction = null; | |
if (strpos($number, '.') !== false) { | |
list($number, $fraction) = explode('.', $number); | |
} | |
switch (true) { | |
case $number < 21: | |
$string = $dictionary[$number]; | |
break; | |
case $number < 100: | |
$tens = ((int) ($number / 10)) * 10; | |
$units = $number % 10; | |
$string = $dictionary[$tens]; | |
if ($units) { | |
$string .= $hyphen . $dictionary[$units]; | |
} | |
break; | |
case $number < 1000: | |
$hundreds = (int)($number / 100); | |
$remainder = (int)($number % 100); | |
if ($hundreds == 1) { | |
$string = $dictionary[100]; | |
} else { | |
$string = $dictionary[$hundreds] . ' ' . $dictionary[100]; | |
} | |
if ($remainder) { | |
$string .= $conjunction . convert_number_to_words((int)$remainder); | |
} | |
break; | |
default: | |
$baseUnit = pow(1000, floor(log($number, 1000))); | |
$numBaseUnits = (int) ($number / $baseUnit); | |
$remainder = $number % $baseUnit; | |
if ($numBaseUnits == 1) { | |
$string = $dictionary[1000]; | |
} else { | |
$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit]; | |
} | |
if ($remainder) { | |
$string .= $remainder < 100 ? $conjunction : $separator; | |
$string .= convert_number_to_words($remainder); | |
} | |
break; | |
} | |
if (null !== $fraction && is_numeric($fraction)) { | |
if (strlen($fraction) == 1) { | |
$fraction = $fraction * 10; | |
} | |
$fraction_str = strval($fraction); | |
if (strlen($fraction_str) > 1 && $fraction_str[0] == 0) { | |
$fraction = $fraction[1]; | |
} | |
$string .= $big_currency . convert_number_to_words($fraction) . $sma_currency; | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment