Thread Starter
sallam
(@sallam)
Here is what I’m trying to do:
I’m creating a calculator for people to calculate the cost of their monthly gas consumption. There are 3 categories with different prices.
1- first 25 meters cost 0.4 each
2- from 26 to 50 meters, the price is 1 per meter of gas
3 over 50 meters cost 1.50 for each meter
So it seems that I need 3 nested IF’s, no?
How can I do this in that plugin please? what is the proper equation?
Many thanks for your time.
Thread Starter
sallam
(@sallam)
Reading through the posts, I came up with this formula (please correct it if anything should be coded better):
(function(){
if(fieldname1 < 26) return fieldname1*0.40;
if(fieldname1 >= 26 && fieldname1 <= 50) return ((fieldname1-25) * 1) + 10;
else return ((fieldname1 – 50) * 1.50) + 35;
})()
Is there a better way to do this?
I also noticed that if I input 3 in the form, it returns 1.2000000000000002 not 1.2 as it should. Why is that please?
Hi,
That if should work, the syntax accepted for the formulas is basically a valid JavaScript syntax.
Use the prec(x,y) function to round the “x” number to a “y” number of decimal digits, example:
(function(){
if(fieldname1 < 26) return prec(fieldname1*0.40,2);
if(fieldname1 >= 26 && fieldname1 <= 50) return prec(((fieldname1-25) * 1) + 10,2);
else return prec(((fieldname1 – 50) * 1.50) + 35,2);
})()
… the above code may be optimized to look clearer but it will work if you keep is as a valid JavaScript code.
Thank you for using the plugin!