3

Example code:

function number_from_spelledout_number($number_string)
{
    $a = new \NumberFormatter('en_US', \NumberFormatter::SPELLOUT);
    return $a->parse($number_string);
} 

var_dump(number_from_spelledout_number('one'));
var_dump(number_from_spelledout_number('two'));
var_dump(number_from_spelledout_number('twenty'));
var_dump(number_from_spelledout_number('twenty-one'));
var_dump(number_from_spelledout_number('twenty one'));
var_dump(number_from_spelledout_number('twentyone'));

Expected/desired output:

float(1)
float(2)
float(20)
float(21)
float(21)
float(21)

Actual output:

float(1)
float(2)
float(20)
float(21)
float(2001)
float(20)

As you can see, it only understands specifically the format "twenty-one" to mean "21"; both "twenty one" and "twentyone" confuse it into the wrong numbers.

While I would personally always type "twenty-one", I know people: they never type the way you would want them to. They will very obviously type "twenty one" and "twentyone" and many other variations, and that's assuming they even get the spelling right. I would not be surprised to get input such as "twenti too" or "tve enty 2" etc. in "the wild". And then this thing breaks.

Is it possible to (easily) make this more intelligent/solid/less strict somehow?

user16508174
  • 281
  • 1
  • 6
  • Maybe this helps [php-numberformatter-spellout-is-not-providing-required-format-in-english](https://stackoverflow.com/questions/57370234/php-numberformatter-spellout-is-not-providing-required-format-in-english) – wayneOS Jul 28 '21 at 08:24
  • @wayneOS Doesn't seem related at all? – user16508174 Jul 28 '21 at 08:33

0 Answers0