Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Comments on Convert integer to English [FINALIZED]

Post

Convert integer to English [FINALIZED]

+2
−0

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).

When there are several variants of writing the number in English, you can choose whichever suits you more. For example, the input $120$ may give the output one hundred twenty or one hundred and twenty.

The numbers must be formatted as follows:

  • Two-digit numbers must be written with dashes where applicable e.g. twenty-five, not twenty five.

  • powers of ten start with “one”, e.g. one hundred, not just hundred.

  • The tens/ones part, if not zero, must be separated from the hundreds part with and, e.g. two hundred and five, not two hundred five.

  • The above rules are applied also to the factors of thousand and million.

  • moreover the global tens/ones part has bto 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
History
Why does this post require moderator attention?
You might want to add some details to your flag.

2 comment threads

Reference implementation is incorrect (5 comments)
General (3 comments)
Reference implementation is incorrect
Moshi‭ wrote over 2 years ago

If I am understanding these points correctly,

The tens-/ones part, if not zero, must be separated with and, e.g. two hundred and five, not two hundred five.

The above rules are applied also to the factors of thousand and million.

Then 1,000,001 should be "one million and one"; however, your reference implementation gives "one million one".

celtschk‭ wrote over 2 years ago

Well, actually that is a case I didn't consider. On one hand, the wording is obviously misleading because that's not what it was referring to, on the other hand I think “one million and one” is the reasonable result here. I'll have to think about how to adapt the rules/reference implementation accordingly.

celtschk‭ wrote over 2 years ago · edited over 2 years ago

OK, I've changed both the reference implementation and the wording, and added more test cases. Can you please check again (in particular the wording; English is not my native language, therefore any suggestions for improvement are welcome)?

Moshi‭ wrote over 2 years ago

celtschk‭ "On one hand, the wording is obviously misleading because that's not what it was referring to" What was it referring to then? I'm not sure what else it could mean.

celtschk‭ wrote over 2 years ago

Moshi‭ It was referring to the individual three-digit group.