Skip to content

Instantly share code, notes, and snippets.

@bzikarsky
Created December 21, 2010 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bzikarsky/750568 to your computer and use it in GitHub Desktop.
Save bzikarsky/750568 to your computer and use it in GitHub Desktop.
Codegolf: Convert numbers up to 999'999 into german numbers
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