0

EG:

  • 1 -> first
  • 2 -> second
  • 3 -> third

and so on.

I looked at ICU4J, but it only has the option to convert to ordinal numbers, eg:

  • 1 -> 1st
  • 2 -> 2nd
  • 3 -> 3rd

But this is not what I want.

Solubris
  • 3,603
  • 2
  • 22
  • 37
  • There is no built-in API for that, but as long as you only care about doing it in English, it’s fairly straightforward. – VGR Jun 26 '20 at 01:07
  • 2
    Is this for a limited set of numbers like 1-10, or any arbitrary number? – jarmod Jun 26 '20 at 01:11
  • 21 is for `secondfirst` or `twentyfirst`? Provide more examples on hundreds, thousand etc. please – Vladimir Shefer Jun 26 '20 at 01:19
  • 1
    Does this answer your question? [Is there a way in Java to convert an integer to its ordinal?](https://stackoverflow.com/questions/6810336/is-there-a-way-in-java-to-convert-an-integer-to-its-ordinal). Specifically, you can get the ordinals as words using `RuleBasedNumberFormat` from `ICU4J`: `rbnf.format(i, "%spellout-ordinal")`. – andrewJames Jun 26 '20 at 01:22
  • In particular, [this answer](https://stackoverflow.com/a/33566603/3558960) to the duplicate question shows how it can be done using ICU4J. – Robby Cornelissen Jun 26 '20 at 01:23
  • Just to add: I'm not entirely convinced by how the library handles things from a locale point of view. The example in the linked answer says it uses a UK locale, but it gives (for example) _one hundred one thousand three hundred eighty-second_ for the number 101,382. I would expect it to be _one hundred and one thousand three hundred and eighty-second_. That would (I think) be more natural for the UK. – andrewJames Jun 26 '20 at 01:33

1 Answers1

-1

I would just do it with a custom Enum. Multiple ways you could do it, so this is just a rough example to PROVE the point.

import java.util.*;
import java.util.stream.*;

public class HelloWorld{

    enum PhrasedNumber {
       Zeroth, First, Second, Third, Fourth;
       static List<String> enumNames = Stream.of(PhrasedNumber.values())
                               .map(PhrasedNumber::name)
                               .collect(Collectors.toList());
    }
       

     public static void main(String []args){
        String phrase = PhrasedNumber.enumNames.get(2);
        System.out.println("Hello World " + phrase);
     }
}

The output looks like this:

Hello World Second
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • Completely unclear what is the solution and how it relates to the question – Vladimir Shefer Jun 26 '20 at 01:20
  • 1) Your example will not compile. 2) If it would compile, `PhrasedNumber.values[2]` would return `Third`, which seems counter-intuitive. 3) Yes, of course you can store all possible integer values in an enum, or a hashmap, or a text file, or a database. That's not a scalable solution. – Robby Cornelissen Jun 26 '20 at 01:32
  • Instead of being so critical, provide your own answer. Apparently, I'm the only one here trying to answer the question. – djangofan Jun 26 '20 at 01:35
  • I found an exact duplicate to the question, and linked to the answer that contains exactly the functionality OP wants in the comments. But if you want to feel like you're the only one trying to contribute here, be my guest. Also, I think that pointing out that your code doesn't compile qualifies as being "so critical". – Robby Cornelissen Jun 26 '20 at 01:40