Comments on Convert integer to English
Parent
Convert integer to English
Given a non-negative integer up to $999\,999\,999$, write it in English.
The input number can be in any form other than English (though you'll typically want to use the native integer type of your language).
The numbers must be formatted as follows:
-
Two-digit numbers must be written with dashes where applicable e.g.
twenty-five
, nottwenty five
. -
powers of ten start with “one”, e.g.
one hundred
, not justhundred
. -
The tens/ones part, if not zero, must be separated from the hundreds part with
and
, e.g.two hundred and five
, nottwo hundred five
. -
The above rules are applied also to the factors of thousand and million.
-
moreover the global tens/ones part has to be separated by
and
whenever anything larger is present, even if the hundreds part is missing. -
Adjacent words must be separated by a single space, or a single dash without space where applicable.
-
Leading and/or trailing spaces are not allowed. However, if the result is printed, a single trailing newline (not preceded by any spaces) is allowed.
Ungolfed reference implementation
Test cases:
0 zero
1 one
4 four
10 ten
16 sixteen
42 forty-two
125 one hundred and twenty-five
1024 one thousand and twenty-four
1100 one thousand one hundred
12345 twelve thousand three hundred and forty-five
1000000 one million
1000001 one million and one
7023000 seven million twenty-three thousand
11012021 eleven million twelve thousand and twenty-one
999999999 nine hundred and ninety-nine million nine hundred and ninety-nine thousand nine hundred and ninety-nine
[Sclipting], (UTF-16) 454 406 …
3y ago
[Python 3], 399 bytes ``` …
3y ago
[Python 3], 596 589 585 482 46 …
3y ago
Post
Python 3, 596 589 585 482 468 467 429 bytes
def f(x):
s="";i="r fif six seven eigh nine";a=f"zero one two three four five six seven eight nine ten eleven twelve thir fou{i} twen thir fo{i}".split();t=1000;c=t*t
if x>=c:s=f(x//c)+" million";x%=c
if x>=t:s+=" "*(s>"")+f(x//t)+" thousand";x%=t
if s*x:s+=" "
n=x>99
if n:s+=a[x//100]+" hundred";x%=100
if s*x:s+=" "*n+"and "
return x<20 and s+a[x]*(not(x<1)*s)+"teen"*(x>12)or s+a[x//10+18]+"ty"+("-"+a[x%10])*(x%10>0)
Golfed 7 bytes thanks to @celtschk's advice. Golfed a massive 103 bytes thanks to @Moshi's advice. Golfed another 14 bytes thanks to @Moshi's advice. Golfed 38 bytes thanks to @celtschk's advice.
1 comment thread