Skip to main content
Inspiring
August 13, 2018
Answered

Number to German Word

  • August 13, 2018
  • 1 reply
  • 474 views

Hey,

I need a Function which will convert any given number to the german words like 820 = "achthunderundzwanzig"

I've found an english version which works for e.g. 820 (I made a few changes to represent the german words for the numbers) except if the number contains ones at the end like 825 or 929 then it shows the word like: "neunhundertzwanzigneun" which should be of course "neunhundertneunundzwanzig"

function int_to_words(int) {   if (int === 0) return 'zero';    var ONES  = ['','eins','zwei','drei','vier','fünf','sechs','sieben','acht','neun','zehn','elf','zwölf','dreizehn','vierzehn','fünfzehn','sechzehn','siebzehn','achtzehn','nunzehn'];   var TENS  = ['','','zwanzig','dreizig','vierzig','fünfzig','sechzig','siebzig','achtzig','neunzig'];   var SCALE = ['','tausend','millionen','billionen','trillionen','quadrillionen','quintillionen','sextillionen','septillionen','octillionen','nonillionen'];    // Return string of first three digits, padded with zeros if needed   function get_first(str) {     return ('000' + str).substr(-3);   }    // Return string of digits with first three digits chopped off   function get_rest(str) {     return str.substr(0, str.length - 3);   }    // Return string of triplet convereted to words   function triplet_to_words(_3rd, _2nd, _1st) {     return (_3rd == '0' ? '' : ONES[_3rd] + 'hundert') + (_1st == '0' ? TENS[_2nd] : TENS[_2nd] && TENS[_2nd] + '' || '') + (ONES[_2nd + _1st] || ONES[_1st]);   }    // Add to words, triplet words with scale word   function add_to_words(words, triplet_words, scale_word) {     return triplet_words ? triplet_words + (scale_word && '' + scale_word || '') + '' + words : words;   }    function iter(words, i, first, rest) {     if (first == '000' && rest.length === 0) return words;     return iter(add_to_words(words, triplet_to_words(first[0], first[1], first[2]), SCALE), ++i, get_first(rest), get_rest(rest));   }    return iter('', 0, get_first(String(int)), get_rest(String(int))); }

Can someone show me what I need to change from the script above?

This topic has been closed for replies.
Correct answer AirMan

This solved my problems: https://github.com/terehov/zahlwortJS/blob/master/index.js

1 reply

AirManAuthorCorrect answer
Inspiring
August 17, 2018