Number formatting a function value (currency)

I am using the Advanced Custom Fields wordpress plugin and I am outputting the value of one of the fields: the_field('Price'); which gives a number .

I want to format this number as pounds sterling with commas.

Read More

I am having some problems outputting it. It seems to be to do with which comes first the outputted function value or the number format function.

$money = the_field('Price');
echo  '£' . number_format($money,0, '.', '');

This doesn’t work and outputs e.g. 300000£0

thanks in advance.

Related posts

Leave a Reply

3 comments

  1. It looks like the_field() doesn’t return anything but echoes instead.

    You can write your own custom function:

    function my_the_field($field, $post_id = false) {
        $value = get_field($field_name, $post_id);
        if (is_array($value)) {
            $value = @implode(', ', $value);
        }
        return $value;
    }
    

    and use that in place of the_field()

    or capture output using ob_start() and ob_get_clean() and pass it to number_format()

  2. Thanks for the excellent answer:

    the second option worked:

    ob_start();

            the_field('Price');
    
            $out = ob_get_clean();
            $out = strtolower($out);
    
           echo '£' .  number_format($out);
    
           // var_dump($out);