Jump to content

Formatting Post Numbers


Recommended Posts

Dan is correct. Once you start formatting it you run into internationalization issues (. vs ,).

 

The only place it bothers me at all is in the admin area, where we have stats and such and with larger numbers (in the millions.) That's a little hard to read, but nothing major.

Don't forget spaces, Philip: There are a few of us who're using them as thousands separators too. :P

 

Though, that said it's (should be) quite easy to add a selectable pattern for the number format, just like with the date. Depending upon how the actual code looks for the forum looks like, and if anyone has any time and desire to make it.

I know I don't. :P

 

As far as I care the post count could just be hidden in the profile or something, or not shown at all. The only thing I care about at all is the number of posts per day, and that's only for fun. Shame it disappeared in the forum switch, but... :shrug:

We use comma to separate numbers, as well as the word "eller" (same as "or" in English). Just like with any other list. ;)

2 numbers: 123 eller 456 789
3 numbers: 123, 456 eller 789
2 numbers, with decimals: 12,3 eller 45 678,9
3 numbers, with decimals 1 000,4, 223 342,88 eller 1,2

See, perfectly logical. No? :P

I dare you:

<?php 

//new and improved recursive version of my last attempt: 
function dec2english($number) 
{ 
    if ( !is_string($number) && !is_float($number) && !is_int($number) ) 
    { 
        return false; 
    } 

    if ( is_string($number) ) 
    { 
        //we know it's a string.  see if there's a negative: 
        if ( substr($number, 0, 1) == '-' ) 
        { 
            $number = substr($number, 1); 
            $number = $number * -1; 
        } 
    } 
    $number = strval($number); 

    if ( $number == '0' ) 
    { 
        return "Zero"; 
    } 
    $negative = $number < 0 ? "Negative" : ""; 

    $number = trim($number, '-'); 

    $split_by_decimal = explode(".", $number); 
    $decimal_string = ''; 
    if ( count($split_by_decimal) > 1 ) 
    { 
        $decimal_string = process_decimal($split_by_decimal[1]); 
    } 
    return trim(preg_replace("#\s+#", " ", $negative . " " . process_number($split_by_decimal[0]) . " " . $decimal_string)); 
} 

function process_number($number, $depth = 0) 
{ 
    $group_designators = array( 
        "", 
        "Thousand", 
        "Million", 
        "Billion", 
        "Trillion", 
        "Quadrillion", 
        "Quintillion", 
        "Sextillion", 
        "Septillion", 
        /*that's enough for now*/); 

    $numbers = array( 
        '1'=>"One", 
        '2'=>"Two", 
        '3'=>"Three", 
        '4'=>"Four", 
        '5'=>"Five", 
        '6'=>"Six", 
        '7'=>"Seven", 
        '8'=>"Eight", 
        '9'=>"Nine", 
        '10'=>"Ten", 
        '11'=>"Eleven", 
        '12'=>"Twelve", 
        '13'=>"Thirteen", 
        '14'=>"Fourteen", 
        '15'=>"Fifteen", 
        '16'=>"Sixteen", 
        '17'=>"Seventeen", 
        '18'=>"Eighteen", 
        '19'=>"Nineteen", 
        '20'=>"Twenty", 
        '30'=>"Thirty", 
        '40'=>"Forty", 
        '50'=>"Fifty", 
        '60'=>"Sixty", 
        '70'=>"Seventy", 
        '80'=>"Eighty", 
        '90'=>"Ninety", 
        ); 

    //we already know that we have a numeric string.  Process it in groups of three characters 
    while ( strlen($number) > 0 ) 
    { 
        if ( strlen($number) <= 3 ) 
        { 
            $number_to_process = $number; 
            $number = ''; 
        } 
        else 
        { 
            $number_to_process = substr($number, strlen($number) - 3); 
            $number = substr($number, 0, strlen($number) - 3); 
        } 

        if ( strlen($number_to_process) == 3 ) 
        { 
            $output[] = $numbers[substr($number_to_process, 0, 1)]; 
            $output[] = "Hundred"; 
            $number_to_process = substr($number_to_process, 1); 
        } 

        if ( isset($numbers[$number_to_process]) ) 
        { 
            $output[] = $numbers[$number_to_process]; 
        } 
        else 
        { 
            //we're dealing with a number greater than 20 and not divisible by 10: 
            $tens = substr($number_to_process, 0, 1) . "0"; 
            $ones = substr($number_to_process, 1, 1); 
            $output[] = isset($numbers[$tens]) ? $numbers[$tens] : '';
            $output[] = $numbers[$ones]; 
        } 
        return process_number($number, $depth+1) . " " . implode(" ", $output) . " " . $group_designators[$depth]; 
    } 
} 

function process_decimal($number) 
{ 
    $suffix = array( 
        "Tenths", 
        "Hundreths", 
        "Thousandths", 
        "Ten Thousandths", 
        "Hundred Thousandths", 
        "Millionths", 
        //enough 
        ); 
    return " and " . process_number($number) . $suffix[strlen($number) - 1]; 
} 

echo dec2english("-19832498347.34") . "\n"; 
echo dec2english(14.12) . "\n"; 
echo dec2english(1432489723485) . "\n"; 
echo dec2english(10234.45645) . "\n"; 
echo dec2english(-10.2) . "\n"; 
echo dec2english("1298721498732.111111") . "\n";

I vote for converting to binary. The presentation display for that is the same for everyone, right?

 

echo base_convert($postCount, 10, 36);

 

For ultimate geekiness: if we use a custom BigInteger library we can even go as far as base-62.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.