Talking About Numbers
InterviewTime limit1sMemory limit1024 MB
Given non-negative integers up to 999,999,999, print each one in English words, following the rules for hyphens, commas, and "and".
- Level
Easy3 of 10
- Topics
- String, Math, Implementation
- Solved
- No attempts yet
Problem
The programmers of a TTS (text-to-speech) program found that their product reads numbers poorly. It announces each digit in turn, so the listener loses track after a few digits.
They want a more natural reading. One of them suggested converting numeric strings into English text, for example turning "" into "one thousand and twenty three", so the speech engine can read the result with no further changes.
Write a program that converts non-negative integers into conventional English wording.
- The numbers to have unique names: "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen".
- The following multiples of are "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety".
- A multiple of ten combined with a digit to is hyphenated, for example "thirty-one" and "seventy-seven".
- Multiples of use the digit name followed by "hundred", and any non-zero remainder after them is joined with "and", for example "two hundred", "four hundred and twelve", and "seven hundred and seventy-seven".
- Thousands and millions are named with the rules above for numbers to . A non-zero remainder after them is separated by a comma, for example "one million, two hundred and fifty-three thousand, one hundred and one".
- If a number with a non-empty thousands or millions part is followed by a remainder of to , the parts are joined with "and" instead of a comma, for example "one million and eleven" and "twenty million, two hundred and twenty-two thousand and forty-three".
Input
The input has one or more datasets. Each dataset is one line containing a non-negative integer from to . The input has no commas, and positive numbers have no leading zeros.
A line with a negative value ends the input.
Output
For each dataset, print the spelled-out number on its own line.
The output must be left-justified and all letters are lowercase. Exactly one blank separates adjacent words, except around a hyphen or comma. A comma is followed by exactly one blank. A hyphen has no blank on either side.