Skip Navigation

[Resolved] Rounding to 2 decimal places

This thread is resolved. Here is a description of the problem and solution.

Problem:
Rounding to 2 decimal places

Solution:
To round the number to two decimal places, you need to create custom shortcode and add it to your current theme's functions.php file.

you can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/rounding-to-2-decimal-places/#post-1074827

Relevant Documentation:

This support ticket is created 6 years, 3 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by stephanieK-2 6 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1074275

Tell us what you are trying to do?
I'd like to round a number to 2 decimal places

This means that if the number is 3 decimal places or more, it should round to 2 decimal places.

It also means that if someone enters 1 decimal place, then a zero should be added.

Is there a standard function as part of Views?

#1074745

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - to round the number to two decimal places, you need to create custom shortcode and add it to your current theme's functions.php file.

Please try to add following code to your current theme's functions.php file:

add_shortcode('round_the_number', function( $atts = [], $content=null ) { 

$atts = shortcode_atts( array('round' => 0 ), $atts ); 

$content = wpv_do_shortcode($content); 
return round($content, $atts['round']); 
});

Call the shortcode as given under:

[round_the_number round='2']

[types field="your-field-name"]

[/round_the_number]
#1074801

Hi - Thanks for that.

A couple of questions...
Did you mean:

[round_the_number round='2'][types field="price"][/types][/round_the_number]

(with the [/types] - as the editor didn't seem to want to accept it without.

I have the field set up as a number, as per the screen shot here: hidden link

It still doesn't seem to work (see: the right hand column under price here: hidden link)

Thanks!
Stephanie

#1074806

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The placeholder is the simple text or anything that will take place when field value is empty.

How do you want to display the following price?
0.12 USD
1.42 SGD
5 MYR

#1074807

As per my original request (which I am re-including below with examples):

I'd like to round a number to 2 decimal places

This means that if the number is 3 decimal places or more, it should round to 2 decimal places.
eg. 2.346 should round to 2.35

It also means that if someone enters 1 decimal place, then a zero should be added.
eg. 1.2 should become 1.20

So following a similar rule, 5 should be 5.00

#1074827

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

ok - please try to use following code and try to resolve your issue:

add_shortcode('round_the_number', function( $atts = [], $content=null ) { 
 
$atts = shortcode_atts( array('round' => 0 ), $atts ); 
 
$content = wpv_do_shortcode($content); 
$number = round($content, $atts['round']); 

return number_format($number,2);
});

To add zero you need to use number_format() function and I've adjusted the code as above and checked the conditions you shared and tested it and its working fine.

Could you please confirm it works at your end as well.

[round_the_number round='2'][types field="price"][/types][/round_the_number]
#1074829

Very good. Many thanks.
I was thinking that this would have been a function as a part of number type, but this works. Thank you !