• Resolved ebarcena

    (@ebarcena)


    So I made a form to calculate hours and I got it working fine. You can see it here:

    https://gleam.guru/time-calculator/

    What I need now is to figure out how to round the calculated number to the nearest half hour based on 15 mn (.25) increments. For example:

    2.24 should round to 2.00
    2.25 to 2.50
    2.74 to 2.50
    2.75 to 3.00

    Here is the equation I’m using:

    PREC( ((120+fieldname3+fieldname4)/60)+((120+fieldname3+fieldname4)/60)*fieldname5 , 2)

    Any help will be greatly appreciated

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @ebarcena,

    My apologies for the delay in respond to your question, however, I’m confused with the examples in your ticket, because if you want to apply a rounding function based on 0.25, the 2.25 should be left as it is 2.25, and not 2.50, in a same way the 2.24 is nearest to 2.25 than 2.00, so, I guess that you are rounding based on 0.5 instead of 0.25

    So, you simply should edit the equation as follows:

    (function(){
    
    function special_round(v)
    {
    	var d = 0;
    	if(v != parseInt(v))
    	{
    		var d = parseInt((v-parseInt(v))*100)/100;
    		if(d < 0.25) d = 0;
    		else if(d < 0.75) d=0.50;
    		else d = 1;
    	}	
    	return parseInt(v)+d;
    };
    
    return PREC( special_round(((120+fieldname3+fieldname4)/60)+((120+fieldname3+fieldname4)/60)*fieldname5) , 2);
    
    })()

    If you need additional help we can offer you a custom coding service from my personal website:

    https://cff.dwbooster.com/customization

    Best regards.

    Thread Starter ebarcena

    (@ebarcena)

    Hello and thanks for your help.

    The examples are correct. The reason I want to round like that is that I’m using the form so customers can calculate the amount of time they need to book for house cleaning services. So technically, .25 is the decimal conversion of 15 minutes.

    It makes more sense (and more fair price wise) to round based on 15 mins increments than a half an hour. To make it more clear:

    2.24 (under 2 hrs 15 mins) should round back to 2 hours
    2.25 (2 hrs 15 mns or more) should round to 2.50 (2 hours 30 mins)
    2.74 (under 2 hrs 45 mins) should round back to 2.50 (2 hours 30 mins)
    2.75 (2 hrs 45 mins or more) should round to 3 hours

    I’m basically trying to replicate the modal on this page:
    https://www.homeaglow.com/customer/choosedate
    (click on the See recommended hours link)

    Thanks

    • This reply was modified 7 years, 8 months ago by ebarcena.
    Plugin Author codepeople

    (@codepeople)

    Hello @codepeople,

    Have you tested the equation I sent you in the previous ticket? It works just like your demos.

    Best regards.

    Thread Starter ebarcena

    (@ebarcena)

    Yes. It works

    Thanks again

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Specific rounding’ is closed to new replies.