Skip Navigation

[Closed] Format number with commas

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 4 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1662107

Hello Christian,

Thank you so much for providing me detailed instructions on how to set up calculations fields.
I think that Toolset should seriously introduce an easier way to add calculations by the way.

To be honest, I found easier to use the old Views compared to Toolset Blocks, I needed a table to show few columns and a total calculation and the loop wizard + some hand coding by myself allowed to create the table I wanted.
Just to give you an idea this is wanted I wanted to achieve:
hidden link

Last question how can I get in the total field the commas for thousand value? Eg. instead of 1000, this one? 1,000.

Look forward to your reply
Best wishes

#1662109

Again, there is no simple solution for calculating and formatting numbers like this built-in to Toolset. You can try adding another shortcode and wrapping the previous shortcode with this formatting shortcode:
https://toolset.com/forums/topic/table-sorting-numbers-and-currency-not-working-properly/#post-616384

#1666643

Hi Christian,
Sorry for the late reply.

The solution you gave me works for the single entry, each amount in the custom field gets the comma, thank you.
But still, I am getting the total from Total £[show-total][/show-total] without the comma.
Any chance you can provide me with a workaround?

Best wishes

#1667183

Okay I'm not clear from your response - did you try wrapping show total in the formatting shortcode, like this?

Total £[ts_format_number][show-total][/show-total][/ts_format_number]
#1667223

I did,
But I get total "0" so is not working.
Cannot show the actual table since is sensitive data.
Cheers

#1668581

Okay please modify the show-total shortcode from the other solution. Here is the updated version:

function show_total_shortcode( $atts, $content ) {
  global $total;
  $atts = shortcode_atts( array(
    'decimals' => 0
  ), $atts );
  $totalNew = $total;
  $total = 0;
  return number_format($totalNew, (int) $atts['decimals'] );
}

add_shortcode('show-total', 'show_total_shortcode');

Now you should be able to remove the format number shortcode wrapping the show-total shortcode, and display the formatted number with any number of decimal places. Here's an updated usage example:

Total £[show-total decimals="2"][/show-total]

Note that the "2" here sets the number of decimals in the rendered output. If this isn't working as expected, please copy + paste the code from your View's loop so I can see how you are using the add-to-total shortcode and how you are displaying each number in the loop. Thanks!

The topic ‘[Closed] Format number with commas’ is closed to new replies.