geil

Php code posted by noogz
created at 16 Feb 21:10

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
  so let's say you are given an input : 3789
 the program should output : "Three thousand seven hundred eighty nine"
 feel free to use pseudo code
 bugs also aren't an issue


The easiest, and quickest way would be to use the PEAR package Numbers_Words
http://pear.php.net/package-info.php?package=Numbers_Words
but i am assuming for this situation you are looking for a manual mapping.

edge cases: 10 - 19

 */

print nameForNumber(3789, 'de')."\n";


/**
 * Translate integer number to string Word version
 * @param Integer $inputString
 * @param String $language: 'en' = english (default), 'de' = german
 * @return String
 */
function nameForNumber($inputString, $language = 'en') {

    $dictionary['en']['std'] = 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 => 'fourty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
    $dictionary['en']['thousand'] = 'thousand';
    $dictionary['en']['hundred'] = 'hundred';

    $dictionary['de']['std'] = array(0 => '', 1 => 'ein', 2 => 'zwei', 3 => 'drei', 4 => 'vier', 5 => 'fuenf', 6 => 'sechs', 7 => 'sieben', 8 => 'acht',
    9 => 'neun', 10 => 'zehn', 11 => 'elf', 12  => 'zwoelf', 13 => 'dreizehn', 14 => 'vierzehn', 15 => 'fuenfzehn', 16 => 'sechzehn', 17 => 'siebzehn',
    18 => 'achzehn', 19 => 'neunzehn',
    20 => 'zwanzig', 30 => 'dreissig', 40 => 'vierzig', 50 => 'fuenfzig', 60 => 'sechzig', 70 => 'siebzig', 80 => 'achzig', 90 => 'neunzig');
    $dictionary['de']['thousand'] = 'tausend';
    $dictionary['de']['hundred'] = 'hundert';



    $thousand = (floor($inputString/1000));
    $reminder = ($inputString % 1000);

    $hundred = (floor($reminder/100));
    $reminder = ($reminder % 100);

    $ten = (floor($reminder/10));
    $one = ($reminder % 10);
        var_dump($one);

    $returnString = "";

    if($thousand >0){
        $returnString .= $dictionary[$language]['std'][$thousand].$dictionary[$language]['thousand'];

    }
    if($hundred >0){
        $returnString .= $dictionary[$language]['std'][$hundred].$dictionary[$language]['hundred'];
    }

    if($ten >1){
        $realTen = (10*$ten);
        if($language == 'en') {
            $returnString .= $dictionary[$language]['std'][$realTen];
            if($one >0) {
                $returnString .= $dictionary[$language]['std'][$one];
            }
        }
        if($language == 'de') {
            if($one >0) {
                $returnString .= $dictionary[$language]['std'][$one]."und";
            }
            $returnString .= $dictionary[$language]['std'][$realTen];

        }

    }else{
      $realTen = ((10*$ten) + $one);
        $returnString .= $dictionary[$language]['std'][$realTen];
        if($realTen == 1 && $language == 'de') {
          $returnString .= 's';
        }
    }
    return $returnString;
}
3.13 KB in 6 ms with coderay