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

Post History

66%
+2 −0
Sandbox Convert integer to English [FINALIZED]

posted 3y ago by celtschk‭  ·  edited 1y ago by trichoplax‭

#8: Post edited by user avatar trichoplax‭ · 2023-06-19T10:25:23Z (over 1 year ago)
Add finalized tag now that the sandbox can be filtered to exclude tags
Convert integer to English [FINALIZED]
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).

<s>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`.</s>

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](https://tio.run/##hVTbbtswDH33V3ACCsRoijpO16LZ2h8J@pAudC1AkQtJbusN@/aUoi6xF2/Tg0VR1DkkdeTXwbWdXh@Pe2wA9YuStl3oclMADYO2Vw4eQIiCHbIBDY8PsKp4hKhJZIa4vo5BJVyCgINUSnZa5BOagjVcJKhz/BM4eSP@F5/KaWPEfEk7IIpz7x8JhWyo5t7u9H4@nZCLPeyU0v3B0s4WxE80nViC6DT6yb3zyrUGed10veFZvvHayg@e8A21N1C@tM4bWkYA759UEoZAlc64d1QBzbXSOAxez5Rt2SSTCLPpAdKCiaPtudmGJ2Z2QyyOmLQbMtMQeJJBJEOiGE4EQ4YfMvjgoRm73dnUZU8yvsBvo7uGCihiEr0p5u82wba93htk1CSWrJ7R9hxMvtNtkMMTqyGemBVDRl5MCurMmKrkGriaiWpns/m/ZIVHG7@471BPgUPjKIn07v71LkZFh2tHZXGuOySHLXBj4ArqqJFpP86S@EtdV4I6OyGOPxTXGx0Di6ND637sLAYVVktYLeGGvt66JbOmuf7qHbV303V4x/qGXTySQQfvqnodPKtqVVc1ue7T8KIkPQMl84wGpIZMzQW8GqndohG/QsDm8f43STr/OthZlsXxEw)

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
```
#7: Post edited by user avatar celtschk‭ · 2021-09-28T19:11:21Z (about 3 years ago)
  • Convert integer to English
  • Convert integer to English [FINALIZED]
#6: Post edited by user avatar celtschk‭ · 2021-09-05T20:37:05Z (about 3 years ago)
Updated wording and reference implementation, added more test cases
  • 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).
  • <s>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`.</s>
  • 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 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.
  • * 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](https://tio.run/##fVNRT@MwDH7vrzCRkJgoYgwOCQT8kYmH3c1dI2UuSlIgd7rfvnOcNGtZuTw0juN8n93Pfgu@7ej2cNhiA0g7o117QYvHCnhZdL3x8AxKVeLQDRC8PMPNUlaKmkQWiOvrHLSAS1Cw18bojlR5QRxMcD5AneIfwdmb8c9iKseLEfMl34CqTr1fEkrZcM2929B2Pp2Ui9tvjKF@7/hmDeo32k7VoDrCuPkPOfnWopybrrey63c5O/0pG74jRQP1rvXRIJ0Bon9SSVoKzfDGf6BJaL7V1mPyRqZi62YwmbCYEWA4CHG2I7fY8CrMPuTimIl8KEwh8QwGk4SBIhwJQoEPBTxE6JGUsAT@zd/IN5VO3O3GtT1tLW6dCJJ6oSCOrudgimTrpPariJ1fzGo9hywZS@7zqcbr8Tw8wWrarKnuzpap@F/XjnJOoqBxOEfMYq1B6oIrWGUFp@WcJDEmHZVwpfjHTIjzuPveUg6sDh6d/7VxmHpkWcNNDXf8jdY9myveV7d3P@phhmt4GFbsAm4gYPyfaEETFDTJ6c1q8heN@pMCHl8e/nIPlVkV52JRHf4B)
  • Test cases:
  • ```
  • 0 zero
  • 1 one
  • 4 four
  • 10 ten
  • 16 sixteen
  • 42 forty-two
  • 12345 twelve thousand three hundred and forty-five
  • 1000000 one million
  • 999999999 nine hundred and ninety-nine million nine hundred and ninety-nine thousand nine hundred and ninety-nine
  • ```
  • 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).
  • <s>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`.</s>
  • 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](https://tio.run/##hVTbbtswDH33V3ACCsRoijpO16LZ2h8J@pAudC1AkQtJbusN@/aUoi6xF2/Tg0VR1DkkdeTXwbWdXh@Pe2wA9YuStl3oclMADYO2Vw4eQIiCHbIBDY8PsKp4hKhJZIa4vo5BJVyCgINUSnZa5BOagjVcJKhz/BM4eSP@F5/KaWPEfEk7IIpz7x8JhWyo5t7u9H4@nZCLPeyU0v3B0s4WxE80nViC6DT6yb3zyrUGed10veFZvvHayg@e8A21N1C@tM4bWkYA759UEoZAlc64d1QBzbXSOAxez5Rt2SSTCLPpAdKCiaPtudmGJ2Z2QyyOmLQbMtMQeJJBJEOiGE4EQ4YfMvjgoRm73dnUZU8yvsBvo7uGCihiEr0p5u82wba93htk1CSWrJ7R9hxMvtNtkMMTqyGemBVDRl5MCurMmKrkGriaiWpns/m/ZIVHG7@471BPgUPjKIn07v71LkZFh2tHZXGuOySHLXBj4ArqqJFpP86S@EtdV4I6OyGOPxTXGx0Di6ND637sLAYVVktYLeGGvt66JbOmuf7qHbV303V4x/qGXTySQQfvqnodPKtqVVc1ue7T8KIkPQMl84wGpIZMzQW8GqndohG/QsDm8f43STr/OthZlsXxEw)
  • 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
  • ```
#5: Post edited by user avatar celtschk‭ · 2021-09-05T19:36:54Z (about 3 years ago)
Added rules about space
  • 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).
  • <s>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`.</s>
  • 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 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.
  • [Ungolfed reference implementation](https://tio.run/##fVNRT@MwDH7vrzCRkJgoYgwOCQT8kYmH3c1dI2UuSlIgd7rfvnOcNGtZuTw0juN8n93Pfgu@7ej2cNhiA0g7o117QYvHCnhZdL3x8AxKVeLQDRC8PMPNUlaKmkQWiOvrHLSAS1Cw18bojlR5QRxMcD5AneIfwdmb8c9iKseLEfMl34CqTr1fEkrZcM2929B2Pp2Ui9tvjKF@7/hmDeo32k7VoDrCuPkPOfnWopybrrey63c5O/0pG74jRQP1rvXRIJ0Bon9SSVoKzfDGf6BJaL7V1mPyRqZi62YwmbCYEWA4CHG2I7fY8CrMPuTimIl8KEwh8QwGk4SBIhwJQoEPBTxE6JGUsAT@zd/IN5VO3O3GtT1tLW6dCJJ6oSCOrudgimTrpPariJ1fzGo9hywZS@7zqcbr8Tw8wWrarKnuzpap@F/XjnJOoqBxOEfMYq1B6oIrWGUFp@WcJDEmHZVwpfjHTIjzuPveUg6sDh6d/7VxmHpkWcNNDXf8jdY9myveV7d3P@phhmt4GFbsAm4gYPyfaEETFDTJ6c1q8heN@pMCHl8e/nIPlVkV52JRHf4B)
  • Test cases:
  • ```
  • 0 zero
  • 1 one
  • 4 four
  • 10 ten
  • 16 sixteen
  • 42 forty-two
  • 12345 twelve thousand three hundred and forty-five
  • 1000000 one million
  • 999999999 nine hundred and ninety-nine million nine hundred and ninety-nine thousand nine hundred and ninety-nine
  • ```
  • 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).
  • <s>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`.</s>
  • 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 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.
  • * 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](https://tio.run/##fVNRT@MwDH7vrzCRkJgoYgwOCQT8kYmH3c1dI2UuSlIgd7rfvnOcNGtZuTw0juN8n93Pfgu@7ej2cNhiA0g7o117QYvHCnhZdL3x8AxKVeLQDRC8PMPNUlaKmkQWiOvrHLSAS1Cw18bojlR5QRxMcD5AneIfwdmb8c9iKseLEfMl34CqTr1fEkrZcM2929B2Pp2Ui9tvjKF@7/hmDeo32k7VoDrCuPkPOfnWopybrrey63c5O/0pG74jRQP1rvXRIJ0Bon9SSVoKzfDGf6BJaL7V1mPyRqZi62YwmbCYEWA4CHG2I7fY8CrMPuTimIl8KEwh8QwGk4SBIhwJQoEPBTxE6JGUsAT@zd/IN5VO3O3GtT1tLW6dCJJ6oSCOrudgimTrpPariJ1fzGo9hywZS@7zqcbr8Tw8wWrarKnuzpap@F/XjnJOoqBxOEfMYq1B6oIrWGUFp@WcJDEmHZVwpfjHTIjzuPveUg6sDh6d/7VxmHpkWcNNDXf8jdY9myveV7d3P@phhmt4GFbsAm4gYPyfaEETFDTJ6c1q8heN@pMCHl8e/nIPlVkV52JRHf4B)
  • Test cases:
  • ```
  • 0 zero
  • 1 one
  • 4 four
  • 10 ten
  • 16 sixteen
  • 42 forty-two
  • 12345 twelve thousand three hundred and forty-five
  • 1000000 one million
  • 999999999 nine hundred and ninety-nine million nine hundred and ninety-nine thousand nine hundred and ninety-nine
  • ```
#4: Post edited by user avatar celtschk‭ · 2021-09-05T08:01:10Z (about 3 years ago)
Following the comments, define fixed number format
  • 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`.
  • Test cases:
  • ```
  • 0 zero
  • 1 one
  • 4 four
  • 10 ten
  • 16 sixteen
  • 42 forty-two
  • 12345 twelve thousand three hundred and forty-five
  • 1000000 one million
  • 999999999 nine hundred and ninety-nine million nine hundred and ninety nine thousand nine hundred and ninety nine
  • ```
  • 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).
  • <s>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`.</s>
  • 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 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.
  • [Ungolfed reference implementation](https://tio.run/##fVNRT@MwDH7vrzCRkJgoYgwOCQT8kYmH3c1dI2UuSlIgd7rfvnOcNGtZuTw0juN8n93Pfgu@7ej2cNhiA0g7o117QYvHCnhZdL3x8AxKVeLQDRC8PMPNUlaKmkQWiOvrHLSAS1Cw18bojlR5QRxMcD5AneIfwdmb8c9iKseLEfMl34CqTr1fEkrZcM2929B2Pp2Ui9tvjKF@7/hmDeo32k7VoDrCuPkPOfnWopybrrey63c5O/0pG74jRQP1rvXRIJ0Bon9SSVoKzfDGf6BJaL7V1mPyRqZi62YwmbCYEWA4CHG2I7fY8CrMPuTimIl8KEwh8QwGk4SBIhwJQoEPBTxE6JGUsAT@zd/IN5VO3O3GtT1tLW6dCJJ6oSCOrudgimTrpPariJ1fzGo9hywZS@7zqcbr8Tw8wWrarKnuzpap@F/XjnJOoqBxOEfMYq1B6oIrWGUFp@WcJDEmHZVwpfjHTIjzuPveUg6sDh6d/7VxmHpkWcNNDXf8jdY9myveV7d3P@phhmt4GFbsAm4gYPyfaEETFDTJ6c1q8heN@pMCHl8e/nIPlVkV52JRHf4B)
  • Test cases:
  • ```
  • 0 zero
  • 1 one
  • 4 four
  • 10 ten
  • 16 sixteen
  • 42 forty-two
  • 12345 twelve thousand three hundred and forty-five
  • 1000000 one million
  • 999999999 nine hundred and ninety-nine million nine hundred and ninety-nine thousand nine hundred and ninety-nine
  • ```
#3: Post edited by user avatar celtschk‭ · 2021-08-31T09:28:54Z (about 3 years ago)
fixed a spelling error in the test cases
  • 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`.
  • Test cases:
  • ```
  • 0 zero
  • 1 one
  • 4 four
  • 10 ten
  • 16 sixteen
  • 42 forty-two
  • 12345 twelve thousand three hundred and fourty-five
  • 1000000 one million
  • 999999999 nine hundred and ninety-nine million nine hundred and ninety nine thousand nine hundred and ninety nine
  • ```
  • 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`.
  • Test cases:
  • ```
  • 0 zero
  • 1 one
  • 4 four
  • 10 ten
  • 16 sixteen
  • 42 forty-two
  • 12345 twelve thousand three hundred and forty-five
  • 1000000 one million
  • 999999999 nine hundred and ninety-nine million nine hundred and ninety nine thousand nine hundred and ninety nine
  • ```
#2: Post edited by user avatar celtschk‭ · 2021-08-31T09:27:59Z (about 3 years ago)
forgot to add the code-golf tag
#1: Initial revision by user avatar celtschk‭ · 2021-08-31T09:27:26Z (about 3 years ago)
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).

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

Test cases:

```
        0  zero
        1  one
        4  four
       10  ten
       16  sixteen
       42  forty-two
    12345  twelve thousand three hundred and fourty-five
  1000000  one million
999999999  nine hundred and ninety-nine million nine hundred and ninety nine thousand nine hundred and ninety nine
```