Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I improve this number2words script
    primarykey
    data
    text
    <pre><code>import sys words = { 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' } tens = [ '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', ] placeholders = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion' ] # segMag = segment magnitude (starting at 1) def convertTrio(number): return ' '.join([words[int(number[0])], 'hundred', convertDuo(number[1:3])]) # convertDuo(number[1:3]) def convertDuo(number): #if teens or less if int(number[0]) == 1: return words[int(number)] #twenty-five else: return tens[int(number[0]) - 1] + '-' + words[int(number[1])] if __name__ == "__main__": string = [] numeralSegments = [] numeral = sys.argv[1] if int(numeral) &lt; 100: print convertDuo(numeral) else: # split number into lists, grouped in threes for i in range (0, len(numeral), 3): numeralSegments.append(numeral[i:i+3]) numeralSegments.reverse() # for every segment, convert to trio word and append thousand, million, etc depending on magnitude for i in range (len(numeralSegments)): string.append(convertTrio(numeralSegments[i]) + ' ' + placeholders[i]) # reverse the list of strings before concatenating to commas string.reverse() print ', '.join(string) </code></pre> <p><strong>Warning</strong>: I'm a total python novice. I'm aware there are probably many times more efficient ways of doing things. I'd appreciate any pointers to them.</p> <p><strong>Edit</strong>: The code currently only works for numbers whose digit counts are multiples of three. I'd appreciate a suggestion for an elegant way to fix that as well. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload