Tell us what you are trying to do?
I want to format numbers with comma and decimal separators - US format (1,234,567.89). I see a couple of support tickets that speak to this. How would I do this for a number entry from a custom post type? Prefer to use Toolset block to display the number with comma/decimal format if possible.
https://toolset.com/forums/topic/comma-format/
https://toolset.com/forums/topic/commas-every-third-number-displayed/
Thank you!
Hi,
Thank you for contacting us and I'd be happy to assist.
It is recommended to store the unformatted numerical values (for example 1234567.89) as custom field values so that numerical comparisons and arithmetic operations can be performed on them (if and when needed).
You can store the numerical values without the decimal separators in a "number" type custom field and then use a custom shortcode to display the numerical value with decimal separators, on the front-end.
As there is no built-in block to change the format, you can register a new shortcode:
( The code snippet below can be included through either Toolset's custom code feature https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ or through the active theme's "functions.php" file )
// Custom shortcode to display formatted number
function wp_format_number( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'number' => '',
),
$atts
);
return number_format($atts['number'],2,'.',',');
}
add_shortcode( 'wp_format_number', 'wp_format_number' );
And after that, you'll be able to show the formatted value using this new shortcode, through a "Fields and Text" block, like this:
[wp_format_number number='[types field='post-number'][/types]']
Note: In this example the Toolset's field shortcode ( [types field='post-number'][/types] ) is used in the new custom shortcode to convert the numerical value from the custom field with slug "post-number" and show the formatted value ( e.g. 1,234,567.89 ).
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi Waqar,
Thank you for the prompt reply! I followed the instructions and it works great!
Regards,
Chris