Skip Navigation

[Resolved] Format number

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 9 months, 3 weeks ago.

Assisted by: Waqar.

Author
Posts
#2680879

Tell us what you are trying to do?
I have a custom number field that is filled in from an Excel-file using WP Import. The number field only validates as a working number if there is no formatting at all. I want to have spaces as 1000-separator for easier reading but that is not allowed. When importing like this, the number field replaces the spaces with a comma and is unable to validate, thus not being useful.

Example: I want it to look like this 1 500 000 but Toolset translates it to this 1,500,000 and the only working solution I've found is this 1500000

This can be seen here:
hidden link

As these numbers are prices, It would be nice if this was possible. How can I achieve this?

#2680937

Hi,

Thank you for contacting us and I'd be happy to assist.

For effective numerical comparisons, it is recommended to store numerical values without formatting data in the database, just as Toolset's number field stores.

And to show those numerical values in a specific pricing format on the front-end, you can register a custom shortcode, like this:


function format_my_number($atts)
{
    $atts = shortcode_atts(
        array(
        'num' => '',
        'sym' => '',
        'sep' => '',
    ),
        $atts
    );
    $num = $atts['num'];
    $sep = $atts['sep'];
  
    return $atts['sym'].' '.number_format($num, 0, '.', $sep);
}
add_shortcode('format-currency', 'format_my_number');

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

This shortcode accepts 3 attributes:

num: numeric value to format
sym: currency symbol to prepend
sep: thousand separator

In your content, you can then use this shortcode like this:


[format-currency num="[types field='number-field-slug'][/types]" sym="$" sep=" "]

In this example, I've called the value from the field with slug 'number-field-slug' to use '$' as the currency symbol and the space as the thousand separator.

regards,
Waqar

#2681109
toolset_view.png

I am not sure I follow. Should I then add this shortcode in Toolset View instead of the single field that picks up the price today? When I try to do it as in the image, nothing is happening.

#2681410

Yes, your understanding is correct.

If you were previously using the 'Single Field' block, you can replace it with the 'Fields and Text' block and then use the new shortcode in that.
( the build-in 'Shortcode' block doesn't handle the complex/nested shortcodes well, at times )

And in your screenshot, I noticed that the target field's slug doesn't seem correct.
( screenshot: hidden link )

Please make sure that the shortcode is using the field's slug and not the name/label.

#2681725
slug.png
numberandfield.png
kortkod.png

"nybilspris" is the slug of the field. The field "Fields and text" breaks everything and the page crashed all the time when trying to work with it. It also seems to cut out most of the shortcode. The best I got was as seen in the picture and that didn't do anything. Using the shortcode doesn't do anything either.

#2681951

Thank you for the screenshots.

I've updated the shortcode to include an empty value check and it is working now.


function format_my_number($atts)
{
    $atts = shortcode_atts(
        array(
        'num' => '',
        'sym' => '',
        'sep' => '',
    ),
        $atts
    );
    $num = $atts['num'];
    $sep = $atts['sep'];
    
    if (!empty($num)) {
    	return $atts['sym'].' '.number_format($num, 0, '.', $sep);
    }
  
    
}
add_shortcode('format-currency', 'format_my_number');