Results 1 to 3 of 3

Thread: number format to word format

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Question number format to word format

    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.
    PHP Code:
    $shows 3;
    $list 1;
    while(
    $list<=$shows){
        echo 
    "<fieldset>";
        echo 
    "<legend>" . [ICODE]$shownumber[/ICODE] ." 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!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    (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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    Rockonmetal (05-08-2008)

  4. #3
    Join Date
    May 2007
    Location
    By the beach
    Posts
    23
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    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:
    PHP 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 $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 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •