Title: Specific rounding
Last modified: June 28, 2017

---

# Specific rounding

 *  Resolved [ebarcena](https://wordpress.org/support/users/ebarcena/)
 * (@ebarcena)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/specific-rounding/)
 * So I made a form to calculate hours and I got it working fine. You can see it
   here:
 * [https://gleam.guru/time-calculator/](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](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/specific-rounding/#post-9275445)
 * Hello [@ebarcena](https://wordpress.org/support/users/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:
 * [http://cff.dwbooster.com/customization](http://cff.dwbooster.com/customization)
 * Best regards.
 *  Thread Starter [ebarcena](https://wordpress.org/support/users/ebarcena/)
 * (@ebarcena)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/specific-rounding/#post-9275857)
 * 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](https://www.homeaglow.com/customer/choosedate)(
   click on the See recommended hours link)
 * Thanks
    -  This reply was modified 8 years, 11 months ago by [ebarcena](https://wordpress.org/support/users/ebarcena/).
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/specific-rounding/#post-9276630)
 * Hello [@codepeople](https://wordpress.org/support/users/codepeople/),
 * Have you tested the equation I sent you in the previous ticket? It works just
   like your demos.
 * Best regards.
 *  Thread Starter [ebarcena](https://wordpress.org/support/users/ebarcena/)
 * (@ebarcena)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/specific-rounding/#post-9278372)
 * Yes. It works
 * Thanks again

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Specific rounding’ is closed to new replies.

 * ![](https://ps.w.org/calculated-fields-form/assets/icon-256x256.jpg?rev=1734377)
 * [Calculated Fields Form](https://wordpress.org/plugins/calculated-fields-form/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/calculated-fields-form/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/calculated-fields-form/)
 * [Active Topics](https://wordpress.org/support/plugin/calculated-fields-form/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/calculated-fields-form/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/calculated-fields-form/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [ebarcena](https://wordpress.org/support/users/ebarcena/)
 * Last activity: [8 years, 11 months ago](https://wordpress.org/support/topic/specific-rounding/#post-9278372)
 * Status: resolved