Skip Navigation

[Closed] Toolset numeric field 2 decimal places

This support ticket is created 4 years, 2 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 7 replies, has 2 voices.

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

Assisted by: Christian Cox.

Author
Posts
#1849721

Hi

For some reason my numeric field which has for example 1.40 stored in it, on the output only displays 1.4.

If the value is 1.43 it displays 1.43. However if it ever ends in a 0 it doesn't display it.

How can I solve this? As it looks odd when using it for currency.

Thanks.

#1849925

How are you outputting the field value - is this in a shortcode, or in a Block of some kind? Can you share the shortcode or block information in your next reply?

#1850265

I am using a shortcode. I am using the following:

[types field='default-payout' format='FIELD_VALUE'][/types]

Thanks for your help! 🙂

#1852513
Screen Shot 2020-11-22 at 7.41.14 AM.png
Screen Shot 2020-11-22 at 7.41.27 AM.png
Screen Shot 2020-11-22 at 7.41.38 AM.png

Okay that's odd, I'm not able to replicate this on a test site using a similar setup. See the attachments here. I have two numeric custom fields storing the values 1.50 and 1.53, and I've displayed them using Types field shortcodes. Do you have any custom code in place that could be filtering these field values before they are displayed?

As a test, can you temporarily deactivate all custom code snippets in Toolset > Settings > Custom Code, deactivate all plugins except Types and Blocks/Views, and activate a default theme like Twenty Twenty? Be sure your Toolset plugins are all up-to-date.

If the problem is not resolved, I'd like to see how this value is stored in the database. Can you check the postmeta table using phpMyAdmin or something similar, find the field value, and take a screenshot for me?

#1852541

Hey Christian

I disabled all custom code and plugins but the issue still persisted. To be honest I think it might well be to do with how we are storing it in the database. I believe someone from Toolset gave me the following code which we are using to ensure numbers store only to 2 decimal places.

The only thing I didn't do is disable custom widgets plugin and my theme as I think I had issues last time I disabled this and enabled. That being said the issue was there before I start using content aware sidebars plugin. (Also I am using GeneratePress theme which I think is used a lot).

Its odd though because when I go to edit the listing the 0 is showing so it must be storing it, its just when it comes to outputting it?

$field_slugs = array(
    'default-payout',
    //here add more field slugs
);
foreach($field_slugs as $field_slug){
    add_filter( 'wpcf_fields_slug_' . $field_slug . '_value_get', 'Decimal_Places_func');
}
function Decimal_Places_func($value){
    return number_format($value, 2, '.', '');
}

Possibly try this and then see if you end up with the same issue?

#1852565

I believe someone from Toolset gave me the following code which we are using to ensure numbers store only to 2 decimal places.
In this case, the value was probably stored in the database initially as 1.5 instead of 1.50. This custom code is a bit deceptive in that it does not necessarily enforce the value 1.5 to be saved in the database as 1.50, it only forces the value of 1.5 stored in the database to be displayed as 1.50 in wp-admin. It does not affect the front-end display of that value with the Types field shortcode. It would still display on the front-end as 1.5 until you save it as 1.50 in wp-admin. So in wp-admin, you could simply update the post to store the full 1.50 in the database instead of 1.5, and then it should display on the front-end correctly.

If you want to enforce storing the value at two decimal places initially, that would require some code that filters the value before or after it is saved in postmeta. The custom code you have in place only filters the display of the value in wp-admin after it is initially saved in whatever format it is entered.

#1862429

Hey Christian

So I did some digging into this, got quite confusing but I found the root of the problem. Although I am certain I found some exceptions to this rule but can't find them now.

It does look like we are initially storing the values as 1.5.

I am guessing Toolset doesn't have like a price formatted field we could use?

Is there a work around for this, I was thinking about using a conditional output to output the 0 but I am no sure what the condition would be on as it can't count the characters.

#1862657

I am guessing Toolset doesn't have like a price formatted field we could use?
Nothing specific for price, unfortunately, or any built-in operators that define how many decimal places to render for a numeric field. I've posted a workaround for similar issues in other tickets, like the custom shortcode here: https://toolset.com/forums/topic/number-field-commas/

You might be able to customize that example for your site's requirements.

The topic ‘[Closed] Toolset numeric field 2 decimal places’ is closed to new replies.