0

Recently, I had to delete enormous amounts of code which I had painstakingly coded but which already existed inside PHP. For example, I had a function to spell out numbers, such as "56" => "fifty-six". PHP has this built in, supporting any locale/language. It was much better than my built-in function. Example:

$a = new \NumberFormatter('en_US', \NumberFormatter::SPELLOUT);
var_dump($a->format('3'));

Outputs:

three

Now I'm potentially facing this issue again. I don't want to hardcode in 1 => "1st", 2 = "2nd", 3 = "3rd", 4 = "4th", etc. as English, only to then find out that PHP can do it automatically for any language! Is that the case?

To be extremely clear, I want to input "2" to a function and have it spit out "2nd" if the locale is en_US, and whatever else if a different language/locale, as appropriate.

1 Answers1

2

Take a look at this answer.

$locale = 'en_US';
$nf = new NumberFormatter($locale, NumberFormatter::ORDINAL);
echo $nf->format($number);
PaulS
  • 850
  • 3
  • 17
  • 1
    If you believe that this is a duplicate, than it should be flagged as such instead of just copying the answer here. – Patrick Q Jan 30 '20 at 13:45