Output numeric field with formatting

When outputting a numeric field there are no options to format according to requirements for thousands separator, decimal point, number of decimals, etc.

The following shortcode can be used to format a number (including where the number is output by a types shortcode):

add_shortcode('number-format', function ($atts = [], $content = null) {

    // provide defaults
    $atts = shortcode_atts(
        array(
            'places'    => 2,
            'decimal'   => '.',
            'thousand'  => ','
        ),
        $atts
    );

    $output = '';

    if ( isset( $content ) ){
        $output = number_format( $content, $atts['places'], $atts['decimal'], $atts['thousand'] );
    }

    return $output;
});

For example, to output a number in the format “23.987,82” you would use

[number-format places='2' decimal=',' thousand='.']23987.82[/number-format]

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments