PHP Number formatting (st/nd/th)

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
This seems very simple but is quite a common and complex task to tackle in PHP, so basically I'm going to show you how to add a suffix to numbers in PHP.

For example,

1 becomes 1st
73 becomes 73rd
34 becomes 34th
782 becomes 782nd

...and so on.

It's simple and can be achieved in 2 lines.

PHP:
<?php
$number = 782;
$numformat = new NumberFormatter('en_GB', NumberFormatter::ORDINAL);
echo $numformat->format($number);
?>

The outcome is:
Code:
782nd

It can be handy for showing things like "You are the X visitor to this website." etc.

Hope it helps.
 
Last edited:

GarettM

Posting Freak
Aug 5, 2010
833
136
This seems very simple but is quite a common and complex task to tackle in PHP, so basically I'm going to show you how to add a suffix to numbers in PHP.

For example,

1 becomes 1st
34 becomes 34th
782 becomes 782nd

...and so on.

It's simple and can be achieved in 2 lines.

PHP:
<?php
$number = 782;
$numformat = new NumberFormatter('en_GB', NumberFormatter::ORDINAL);
echo $numformat->format($number);
?>

The outcome is:
Code:
782nd

It can be handy for showing things like "You are the X visitor to this website." etc.

Hope it helps.

Some thing to keep in mind that this class requires PHP 5.3 or Higher++
 

Nehalem

Aug 31, 2013
293
47
I thought this would be difficult, with many lines etc. But it's actually kinda easy to understand, lol. Didn't think of it on this way.

Thank you! I suppose this works with all languages?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I thought this would be difficult, with many lines etc. But it's actually kinda easy to understand, lol. Didn't think of it on this way.

Thank you! I suppose this works with all languages?
No, just PHP as it's PHP specific.
 

Jemz

xmas tings.
Aug 6, 2013
166
17
Surely there must be a lot less complicated way to do that or is that the only way, just wondering.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Surely there must be a lot less complicated way to do that or is that the only way, just wondering.
Well technically you'd only have to write
PHP:
$numformat = new NumberFormatter('en_GB', NumberFormatter::ORDINAL);
once, and include it in a global PHP file and then you can just keep doing
PHP:
$numformat->format(3433);
as much as you want.

I understand not every web server has the NumberFormatter class installed although it is a default PHP library, so I coded a work around version earlier as I moved a website over to a different host and it did not support NumberFormatter.

PHP:
<?php
function NumFormat($number) {
    $suffixes = array("st" => array(1),
                      "nd" => array(2),
                      "rd" => array(3),
                      "th" => array(4,5,6,7,8,9,0));
 
    if (!is_numeric($number)) return;
 
    foreach ($suffixes as $suffix=>$nums) {
        foreach ($nums as $num) {
            if ($num == (substr($number, -1))) {
                $number .= $suffix;
                break 2;
            }
        }
    }
    return $number;
}

echo 'You are the ' . NumFormat(23) . ' visitor to this website.'; //You are the 23rd visitor to this website.
?>
 
Last edited:

Users who are viewing this thread

Top