How to format a database text value into a number with thousand separators?

  • Jeroen
    Jeroen
    Newbie 6 points
    April 14, 2015 at 5:26 am

    Within the theme it is very easy to display a database value by just put the meta_key within brackets [ .. ]. However my numbers are alle displayed without thousand separators by doing this. Does someone has an outcome to add those separators (preferably dots). Thanks.

    Attachments:
    You must be logged in to view attached files.
  • Mark Fail
    Mark Fail
    Super Guru 13,013 points
    April 14, 2015 at 5:47 am

    the only way to adjust this would be to create a function manually that formats the data, the default setup will simply output whats in the database.

  • Jeroen
    Jeroen
    Newbie 6 points
    April 15, 2015 at 5:06 am

    Hi Mark, thanks for your quick reply! However forgive me when I’m wrong, but in the page setup –> listing search results I can only use HTML or shortcodes. The php-function format_number() seems not to work, do you have a suggestion of a function which I can directly use in this page setup to display the values with thousand separators? Thanks!

  • Mark
    Mark
    Titan 733 points
    April 15, 2015 at 7:49 pm

    Hi Jeroen, you would need to create a new shortcode function to format your custom field and either add it to your child theme functions.php or make it into a plugin.

    Other option would be to use jQuery (and possibly some jQuery plugin like jquery-currency – https://dev7studios.com/demo/jquery-currency/ )

    Let me know if you need some assistance with that and I can talk to my developer.

    Mark

    This reply was awarded the best answer by the topic poster.
  • Jeroen
    Jeroen
    Newbie 6 points
    April 28, 2015 at 8:31 pm

    @Mark, thanks it works now!

    The following shortcode does the work:

    function price_with_thousand_separator( $atts ) {
    global $post;

    $var = floatval(get_post_meta($post->ID,’price’,true));

    return number_format($var,0,’,’,’.’);
    }

    add_shortcode(‘price_thousand_separator’, ‘price_with_thousand_separator’);

  • Alex Morgan
    Alex Morgan
    Super Guru 1,111 points
    April 29, 2015 at 9:20 am

    Thanks for sharing 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

New Business Themes for WordPress 2020

Start your new website today!

View Business Themes