-
-
Save idnovic/ea6132f3aa49209c45698e301322159a to your computer and use it in GitHub Desktop.
Codegolf: Convert numbers up to 999'999 into german numbers
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
Minified, 411 Bytes, with umlauts | |
Run with `php -d display_errors=Off` to hide DEPRECATED errors on 5.3 | |
See OO-Version at <https://gist.github.com/750787> | |
<?function n($n,$a=0){$e=array(2=>'zwan','dreißig')+$d=split(':',':ein:zwei:drei:vier:fünf:sech:sieb:acht:neun:zehn:elf:zwölf');$t=$n%10;return$n<1000?($n<100?($n<20?($n<13?($d[$n].(($n-7)?(($n-1||$a)&&$n-6?'':'s'):'en')):$d[$t].'zehn'):n($t,1).($t?'und':'').$e[$n/10].(ceil($n/10)-4?'zig':'')):n((int)($n/100),1).'hundert'.n($n%100)):n((int)($n/1000),1).'tausend'.n($n%1000);}while($i<10000)echo n(++$i),"\n"; | |
# verbose (some slight changes for readability) | |
function n($n, $prefix=0) | |
{ | |
// base name for each number | |
$d = split(':', ':ein:zwei:drei:vier:fünf:sech:sieb:acht:neun:zehn:elf:zwölf'); | |
// decimal places below 20 are handled separately; 20 and 30 are special cases | |
$e = split(':', '::zwan:dreißig') + $d; | |
$t = $n % 10; | |
// 1-12 | |
// 6: "sech" append "s" | |
// 7: "sieb" append "en" | |
// 1: "ein" append "s" as long we are not in prefix mode | |
if ($n < 13) return $d[$n] . ( | |
($n-7) ? ( // handle 7 | |
($n-1 || $prefix) && $n-6 ? '' : 's' // handle 1 and 6 | |
):'en') | |
; | |
// 13-19 | |
if ($n < 20) return $d[$t] . 'zehn'; | |
// 20-99 | |
if ($n < 100) return | |
n($t,1) . ($t ? 'und' : '') // first place, recursion with $prefix=1 | |
. $e[$n/10] // decimal place | |
. (ceil($n/10)-4?'zig':'')); // special case 30/"dreißig" | |
// 100-999 | |
if ($n < 1000) return | |
n((int)($n / 100), 1) . 'hundert' | |
. n($n % 100)); | |
// 1000-999999 | |
return | |
n((int)($n / 1000), 1) . 'tausend' | |
. n($n % 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment