WordPress.org

Calculated Fields Form[resolved] How to Round to Two Decimals? (14 posts)

1. Mikejuly
Member
Posted 2 years ago #

I am trying to round an output to 2 decimals - all I can achieve though is getting it to round to whole numbers.

Also, is there a way to hide fields?

2. codepeople
Member
Plugin Author

Posted 2 years ago #

Hi,

The Calculated Field allows you to use the operation "prec". The operation "prec" requires two parameters, the second one determines the decimal digits. For example:

prec(0.333333, 2) will round the number to 0.33

Best regards.

3. Mikejuly
Member
Posted 2 years ago #

Thanks for the quick response. That makes sense and I've made some pregress but I haven't arrived yet.

My follow up question is how do I get the output of the form to round to two decimals. For example on this page:

http://domain4testing3.info/balance-calculator/

I want the 'daily interest' and other calculated fields to round to two decimals. I was able to achieve this by creating another 'calculated field' (at the bottom)with the parameters 'prec(fieldname9, 2)' per your instructions.

Is there a way to get the output to round, and if not, is there a way to hide the field I am rounding?

Thanks again.

MJ

4. codepeople
Member
Plugin Author

Posted 2 years ago #

Hi,

You have more than one Calculated Field on your page, but are using the operation "prec" in only one field, it is possible to use the Calculated Field in all of them.

Best regards.

5. wjweb
Member
Posted 2 years ago #

Could you please explain how to set up the outcome to 2 decimals? I tried with the command prec but then i just get an error.

http://mijnrijles.info/mijn-rijbewijs-info/bereken-je-prijs/

the equation i have now is 141+fieldname2*35.50+fieldname5+fieldname6

The outcome has to include 2 decimals.

thanks
WJ

6. codepeople
Member
Plugin Author

Posted 2 years ago #

Hi,

You are in the correct direction, the command "prec" allow you to round a number to 2 decimals digits. Please, transform the equation as below:

prec((141+fieldname2*35.50+fieldname5+fieldname6),2)

If the trouble persist, please send us a contact through our website with a link to the webpage where the calculated field is used.

Best regards.

7. flabrea
Member
Posted 1 year ago #

How can i round to 2 decimals in a function like

(function(){
var v;
if(fieldname7 == 546 && fieldname11 == 1) v = 142.2342;
if(fieldname7 == 546 && fieldname11 == 2) v = 190.4353;
return v*2;
})()

8. codepeople
Member
Plugin Author

Posted 1 year ago #

Hi,

It is very simple. Exists the operation "prec", that accept two parameters, the second one is the decimals numbers in the result. So, you equation would be:

(function(){
var v;
if(fieldname7 == 546 && fieldname11 == 1) v = 142.2342;
if(fieldname7 == 546 && fieldname11 == 2) v = 190.4353;
return prec(v*2,2);
})();

9. flabrea
Member
Posted 1 year ago #

Hi guys, i do have a little problem. I need all results to show up with two decimals. The way shown above works great when the result is rounded. But when i have something like "10 + 20", of course nothing is rounded but i don't see any decimals either. I need all results to end with .XY (like 10.00).

how can this be done? thanks!

10. codepeople
Member
Plugin Author

Posted 1 year ago #

Hi,

Use the "prec" operation. For example:

prec(10+20,2)

11. flabrea
Member
Posted 1 year ago #

``````(function(){
var v;
if(fieldname6 >> 0) v = fieldname12;
else v = 0;

return prec(v*1,2);
})()``````

any idea? thanks!

12. codepeople
Member
Plugin Author

Posted 1 year ago #

Hi,

I've checked your equation and it is working fine from my copy of plugin. Please, be sure to install the latest plugin's update. (Install the plugin's update preserves the forms created previously)

Another solution is change the structure of the equation like follows:

prec((function(){
var v;
if(fieldname6 >> 0) v = fieldname12;
else v = 0;

return v;
})(),2)

Best regards.

13. ryka
Member
Posted 1 year ago #

Hello Your information has been very helpful so far, and the plugin is very good, however I am struggling with the two decimal points. I am sure I am doing something wrong
The equation I have is
(fieldname2/65)*440)

At the moment its 12 decimals points,

And i am having the problem with all fields.

14. codepeople
Member
Plugin Author

Posted 1 year ago #

Hi,

The operation called "prec" (for precission), allow transform a number to the amount of decimal digits preferred. To display the result of your equation with two decimal digits, transform the equation like follow:

prec((fieldname2/65)*440, 2)

The second parameter in the equation is the number of decimal digits.

Best regards.

This topic has been closed to new replies.