WordPress.org

Ready to get started?Download WordPress

Forums

How to generate comma's instead of periods (dots) (6 posts)

  1. luuk64
    Member
    Posted 5 years ago #

    Hi there,

    I would like to display the total number of posts and comments on my (Dutch) weblog. But the problem is: the php-code I use generates numbers with a comma (3,223 posts) instead of a period (3.223 posts).

    Please help. This is the code I use (I'm an absolute php-newbie):

    <?php
    $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
    if (0 < $numposts) $numposts = number_format($numposts);
    $numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
    if (0 < $numcomms) $numcomms = number_format($numcomms);
    $numcats = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories");
    if (0 < $numcats) $numcats = number_format($numcats);
    ?>

    <?php echo $numposts . ' columns
    ' . $numcomms . ' reacties ' . $numcats . ''; ?>

  2. iridiax
    Member
    Posted 5 years ago #

    To see other format options, try a Google search for: number_format

  3. darrinb
    Member
    Posted 5 years ago #

    What is the Dutch number format?

    Going off your example, this should work for you:

    <?php
    $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
    if (0 < $numposts) $numposts = number_format($numposts, 0, ',', '.');
    
    $numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
    if (0 < $numcomms) $numcomms = number_format($numcomms, 0, ',', '.');
    
    $numcats = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories");
    if (0 < $numcats) $numcats = number_format($numcats, 0, ',', '.');
    ?>

    The default number_format() in PHP is in English notation so you have to specify yours specifically.

    You have to specify the following:

    • Number of decimal places (set to "0" here.)
    • The decimal separator (set to "," here)
    • The thousands separator (set to "." here)
  4. darrinb
    Member
    Posted 5 years ago #

    Here's the link to the PHP manual, although it's not terribly user-friendly.

    http://docs.php.net/manual/en/function.number-format.php

  5. luuk64
    Member
    Posted 5 years ago #

    Thank you dbmartin!
    (:

  6. darrinb
    Member
    Posted 5 years ago #

    no prob!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags