Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
PHP Number formatting (st/nd/th)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Markshall" data-source="post: 230585" data-attributes="member: 1872"><p>Well technically you'd only have to write</p><p>[php]$numformat = new NumberFormatter('en_GB', NumberFormatter::ORDINAL);[/php]</p><p>once, and include it in a global PHP file and then you can just keep doing</p><p>[php]$numformat->format(3433);[/php] as much as you want.</p><p></p><p>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.</p><p></p><p>[PHP]<?php</p><p>function NumFormat($number) {</p><p> $suffixes = array("st" => array(1),</p><p> "nd" => array(2),</p><p> "rd" => array(3),</p><p> "th" => array(4,5,6,7,8,9,0));</p><p> </p><p> if (!is_numeric($number)) return;</p><p> </p><p> foreach ($suffixes as $suffix=>$nums) {</p><p> foreach ($nums as $num) {</p><p> if ($num == (substr($number, -1))) {</p><p> $number .= $suffix;</p><p> break 2;</p><p> }</p><p> }</p><p> }</p><p> return $number;</p><p>}</p><p></p><p>echo 'You are the ' . NumFormat(23) . ' visitor to this website.'; //You are the 23rd visitor to this website.</p><p>?>[/PHP]</p></blockquote><p></p>
[QUOTE="Markshall, post: 230585, member: 1872"] Well technically you'd only have to write [php]$numformat = new NumberFormatter('en_GB', NumberFormatter::ORDINAL);[/php] once, and include it in a global PHP file and then you can just keep doing [php]$numformat->format(3433);[/php] 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. ?>[/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
PHP Number formatting (st/nd/th)
Top