1

I have the following function:

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return __('0 View','mm');
    }
    return $count. __(' Views','mm'); 
}

How do I make it format a number to include a comma in the thousandths place? For example, 50000 should become 50,000.

keruilin
  • 16,782
  • 34
  • 108
  • 175
  • What does the question have to do with the code you've posted? You're going to run in to trouble storing a number as a string in your database. You *can* add 1 to "50,000"... but it'll be "50,0001" no "50,001" – Hamish May 03 '12 at 02:04
  • @Hamish: Not in PHP. In PHP, you'll get 50,001. [Because that's just how PHP string incrementing works...](http://codepad.org/MVdEdLQP) up to [a certain point](http://codepad.org/1Bda7tcM). – Ry- May 03 '12 at 02:07
  • @minitech Oh god, how wrong I was. And `$n + 1` is "51". http://codepad.org/DCJD5pLw - btw, is that locale aware? – Hamish May 03 '12 at 02:12
  • @Hamish: It gets better! American PHP: `'50,001' + 1 == 51` but European PHP: `'50,001' + 1 == 51.001` (Unless I'm horribly mistaken, which is not uncommon when it comes to PHP...) – Ry- May 03 '12 at 02:14
  • Ah, so it is locale aware. FFS. Pity no answer actually deals with the *context* of the question. – Hamish May 03 '12 at 02:15

5 Answers5

3

Use number_format():

number_format($number);
Ry-
  • 218,210
  • 55
  • 464
  • 476
Logan Serman
  • 29,447
  • 27
  • 102
  • 141
2

Have a look at number_format.

For example:

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
Ry-
  • 218,210
  • 55
  • 464
  • 476
RadicalRaid
  • 996
  • 9
  • 17
2

You can use number_format.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Simon Arsenault
  • 1,777
  • 17
  • 35
0
$number=number_format($number);
Ry-
  • 218,210
  • 55
  • 464
  • 476
Alasdair
  • 13,348
  • 18
  • 82
  • 138
0

Your number format use was correct to get the nice formatting you are looking for. The issue I think you are running into regarding changing the format in your DB is that you are formatting the number and resaving it over itself.

For example:

$count = 5000;
$count = number_format($count);
//$count is no longer equal to 5000, it is now a string 5,000

Try using a temporary variable, that way your original $count variable won't change, and when you write back to the database, it will still be in the formatting you want.

For example:

$count = 5000;
$tmpCount = 0;
$tmpCount = number_format($count);
// $count is still 5000, and $tmpCount now holds the value 5,000.

An updated version of your code, I'm assuming you only want the meta holding the comma separated value?

    function setPostViews($postID) {
    //Set initial variables
    $count_key = 'post_views_count';
    $tmpCount = 0;
    $count = get_post_meta($postID, $count_key, true);

    if($count == '') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    } else {
        $count++;
        //Set tmpCount variable to hold comma separated value
        $tmpCount = number_format($count);

        //Comma separated value is passed into your function
        update_post_meta($postID, $count_key, $tmpCount);
    }
}

Was that what you were hoping for? or if you're want to only format this number this way when displaying to the user, but the db to remain proper (not comma separated), after querying the db, save both the db value and create a comma separated variable exactly as I've done above. Then on any db functions, refer to the $count variable, and on any user output functions, use the $tmpCount variable.

Again, I hope that answers your question, if it's not, please clarify.

MaurerPower
  • 2,046
  • 7
  • 26
  • 48