Jump to content

[SOLVED] function for number strings


EchoFool

Recommended Posts

I have a function which changes numbers to a position like 1 = 1st 2 = 2nd etc etc

 

But when the value 1 goes into the function it returns "1th". Not sure where it is that i got wrong in my logic... can you spot it ?

 

<?php
function ordinal($cdnl){
    $test_c = abs($cdnl) % 10;
    $ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th'
            : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
            ? 'th' : 'st' : 'nd' : 'rd' : 'th'));
    return $cdnl.$ext;
} 
?>

Link to comment
Share on other sites

<?php

function test($n) {

if($n == '11' ||
$n == '12' ||
$n == '13'
){
$suff = 'th';
}
elseif($n == '1'){ $suff = 'st'; }

elseif($n == '2'){ $suff = 'nd'; }

elseif($n =='3'){ $suff = 'ed'; }

else { $suff = 'th'; }
return $n . $suff;
}

?>

Link to comment
Share on other sites

<?php
function ordinal($num)
{
$unit = $num % 10;

if ($num >= 10 and $num <= 13)
{
	$suffix = 'th';
}
else
{
	switch ($unit)
	{
		case 1:
			$suffix = 'st';
			break;
		case 2:
			$suffix = 'nd';
			break;
		case 3:
			$suffix = 'rd';
			break;
		default:
			$suffix = 'th';
	}
}

return $num.$suffix;
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.