View Full Version : number format to word format
Rockonmetal
05-07-2008, 07:30 PM
I am not so sure, but I think there is a way to get it so numbers like 1, 2, and 21 are displayed like first, second, and twenty first. In the script I have below I just need this to happen in one place. Everywhere else doesn't need it.
$shows = 3;
$list = 1;
while($list<=$shows){
echo "<fieldset>";
echo "<legend>" . $shownumber ." show of the " . $tour['name'] . ".</legend>";
echo "</fieldset>"; /*Theres other stuff after the legend tag, but its not needed so posting that would be wasting space on DD. */
The highlighted code in the legend should be a spelled out number like First, Second, Third, so on...
Thanks upfront!
djr33
05-07-2008, 09:31 PM
(Note: The php blocks don't allow any tags inside them.)
Well, you've got a problem here. English is a messy language with tons of irregularities.
Honestly, the best way is to just create an array with numbers=>words.
array(1=>'first',2=>'second',3=>'third',....);
There is some logic to this, but with that many irregularities coding any reliable function would be tough.
You can save yourself a bit of time like this:
$x = isset($array[$num])?$array[$num]:word($num).'th';
Check if there is a special value for the number, and if so use that; if not, use the word of the number and add TH.
You can probably find a number=>word function somewhere on the net. (Might even be one using PHP, but I don't know.)
On the other hand, there are a lot of oddities anyway, due to phonetics and odd spellings:
five=>fifth. (th is unvoiced, so the v stops buzzing and becomes an F)
twenty=>twentieth (spelling change by just the standard rules of english)
You could program this, but seems like a lot of work.
You CAN do one thing though-- only the last word gets an ending. Thus, you can remove any first part (451=>450+1), and get the first part as regular numbers then the last bit as the ordinal number (Four Hundred Fifty- + first)
Anyway, really, how high does this need to go?
And look for an existing code-- surely someone has done something like this before.
phpsales
05-08-2008, 11:14 PM
If you're trying to just make the number readable in a sentance you could always use the English Ordinal Suffix. E.g. 2nd, 11th, 211th, etc..
Example Code:
/**
* module: englishOrdinalSuffix
* summary: converts int to english ordinal suffix number string, e.g. 3rd, 11th, 3543rd
* params: int x
* returns: string
* author: phpsales @ dynamic drive forum
**/
function englishOrdinalSuffix($x){
$y = ($x%100>10 && $x%100<20 ? 0 : $x%10);
$eos = array("th","st","nd","rd","th","th","th","th","th","th");
return number_format($x).$eos[$y];
}
echo englishOrdinalSuffix(1); //1st
echo englishOrdinalSuffix(11); //11th
echo englishOrdinalSuffix(53); //53rd
echo englishOrdinalSuffix(1002); //1,002nd
echo englishOrdinalSuffix(30311); //30,311th
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.