Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

487
Views
Is this a bug with PHP's NumberFormatter::SPELLOUT? Can it be made smarter/less strict?

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?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!