Skip Navigation

[Résolu] Display a numeric field decimal value as a fraction

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: How can I use decimal values in a numeric field, and how can I convert between displaying fractions and decimals?

Solution:
There is not a simple answer here. One option is to create a select field with each fraction listed in text, and its value predefined as a decimal. For example, the options could be as follows:
Text: 1/2, Value: 0.5
Text: 1/3, Value: 0.333333
Text: 1/4, Value: 0.25
Text: 1/8, Value 0.125
Text: 2/3, Value: 0.666666
Text: 3/4, Value: 0.75
Text: 3/8, Value: 0.375
Text: 5/8, Value: 0.625
Text: 7/8, Value: 0.875

This support ticket is created Il y a 6 années. 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
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 2 réponses, has 2 voix.

Last updated by laurieH Il y a 6 années.

Assisted by: Christian Cox.

Auteur
Publications
#624702
numeric.JPG

I am trying to: Enter a decimal value into a numeric field

Where the issue can be seen: Creating a new record in admin for a CPT (ingredients)

Questions:
1. How are decimal values entered into numeric fields? I am using the Types Beta 2.3-b5 version.
2. How can numbers be entered by the user as a fraction?
3. How can decimal values be displayed to the user as a fraction?

Example:
I am developing a website that contains recipes. A recipe is related to ingredients. An ingredient might be 1/4 cup oil.

Future capability support:
A recipe can be sized to create 4 servings. If servings are increased to 8 I will need to perform math functions to increase the ingredient quantities.

#624742

1. How are decimal values entered into numeric fields? I am using the Types Beta 2.3-b5 version.
The value must start with a number, so in your example above it should be entered as 0.25

2. How can numbers be entered by the user as a fraction?
One option is to create a select field with each fraction listed in text, and its value predefined as a decimal. For example, the options could be as follows:
Text: 1/2, Value: 0.5
Text: 1/3, Value: 0.333333
Text: 1/4, Value: 0.25
Text: 1/8, Value 0.125
Text: 2/3, Value: 0.666666
Text: 3/4, Value: 0.75
Text: 3/8, Value: 0.375
Text: 5/8, Value: 0.625
Text: 7/8, Value: 0.875
...
and so on. This will allow you to capture decimals, which can be used for calculations in code, while displaying fractions for your visitors.

3. How can decimal values be displayed to the user as a fraction?
There isn't a simple answer for displaying decimals as fractions in HTML, and there are different approaches:
hidden link
Where it gets tricky is in the future support for calculations. You'll want to use decimal values to perform those calculations, but then you probably want to convert those back to fractions to display to your visitors. My guess is this will require a significant amount of custom code. Toolset isn't really designed to help with mathematical calculations, so we don't have many built-in tools to do this type of conversion. Here's a quick search result: https://stackoverflow.com/questions/1954018/php-convert-decimal-into-fraction-and-back

We do have a portal available where you can connect with skilled developers who may be able to assist with this type of programming. You can find that portal here: https://toolset.com/contractors/

#624748

It seems that using a Select field would be the simplest solution. This opens up another question so I will create a new ticket. Thank you for your quick response.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.