June 6, 2020: Removed a rogue JS debugger statement.
Let’s say you sell business cards. Orders for these business cards must be placed in increments of 500. If you try to order 499 cards, you’re going to get 500. If you try to order 600, you’re going to get 1000 (the next increment of 500). Do you see how this works?
This snippet provides a way to round field values (particularly for Number, Quantity, and Calculation fields) up to the nearest specified increment. In addition, it provides a way to cap the increment and set a minimum.
- How do I install this snippet?
- Usage Instructions
- Custom Rounding CSS Class
- In closing
- Taking It Further
- Known Limitations
How do I install this snippet?
- You will want to be running at least Gravity Forms v.1.8 though it may work with some older versions.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Copy and paste the snippet into your theme’s functions.php file.
- This snippet requires no other configuration in the code. Continue reading for usage instructions.
Usage Instructions
This snippet is powered by special CSS classes which can be applied to a field via the “CSS Class Name” setting. Here are step by step instructions:
- Open your form for editing.
- Open the fielding settings for the field you would like to round incrementally.
- Navigate to the “Advanced” tab.
- Select the “CSS Class Name” setting and add the “gw-rounding-n” class, replacing n with the increment to which you would like this field to round.
All Options
gw-round-n | round to the nearest increment; will round up or down to the nearest increment example: gw-round-100 would round 1 to 0, 149 to 100, 150 to 200, etc |
gw-round-up-n | round up to the nearest increment; will always round up example: gw-round-up-50 would round 1 to 50, 511 to 100, 149 to 150, etc |
gw-round-down-n | round down to the nearest increment; will always round down example: gw-round-down-25 would round 1 to 0, 26 to 25, 51 to 50, etc |
gw-round-min-n | round up to the specified minimum if not reached example: gw-round-min-50 would round 1 to 50, 49 to 50, 51 and greater would not be rounded |
gw-round-max-n | round down to the specified maximum if exceeded example: gw-round-max-250 would round 251 to 250, 400 to 250, 250 and below would not be rounded |
gw-round-custom-n | round based on a custom function example: See the Custom Rounding CSS Class section. |
What if I’m already using the “CSS Class Name” setting for another CSS class?
No problem. The “CSS Class Name” setting supports multiple classes and that won’t interfere with the special “gw-round-n” class. Just add a space after the last class and add the “gw-round-n” class after it.
Does this work with decimals?
Yup! You can use decimal values in any of your rounding classes (e.g. gw-round-2.5
to round in increments of 2.5
).
Custom Rounding CSS Class
Rounding by Increments also supports custom rounding methods using the gw-round-custom
CSS class. Custom rounding methods require implementing the math to handle the rounding both in PHP and JavaScript. The PHP will go into your theme’s function.php file and for the JavaScript portion we recommend using our free Custom JavaScript plugin.
Below is an example of how you can use a custom rounding function to round to the nearest 9th number. First is the PHP portion that handles the calculation on the back-end:
// Include the following code in your theme's functions.php or using a custom snippet manager.
add_filter( 'gw_round_nearest9', function( $value, $rule ) {
return round( $value / 10 ) * 10 - 1;
}, 10, 2 );
And this is the JavaScript to handle the calculation on the front-end:
window.gform.addFilter( 'gw_round_nearest9', function( value, rule ) {
return Math.round( value / 10 ) * 10 - 1;
});
The filter’s name corresponds to the CSS class you’ll use on the field to activate the custom rounding. Simply take the function name and replace the underscores with dashes for the CSS class. In the case of the example functions above, the CSS class is gw-round-nearest9
.
In closing
You know the drill. If you like it, use it, or improve it, let us know!
Taking It Further
Use Rounding By Increments to Enforce Min/Max Product Totals
Say you have a form where you want to enforce a minimum or maximum total price for a product. For example, say I have a store that sells bulk widgets that has a minimum order of $500. Any number ordered that totals less than $500 is automatically rounded up to $500. On top of that, any amount ordered that totals more than $35000 is automatically rounded down to $35000. Here’s how you would set that up.
First, add a Number field to your form. This is where the user will insert the quantity of widgets they are purchasing.
Then, add a Product field. Give this Product field name like “Widget Total”, set the Field Type to Calculation, and check Disable quantity field. For the calculation, insert the merge tag for the Number field above and multiply it by your unit price.
Then, go the Appearance tab of the Product field and insert the gw-round-min-n
and gw-round-max-n
merge tags. Set the values of the merge tags to your minimum and maximum product total.
If a user buys a quantity that would total less than $500, it’s automatically rounded up, and vice versa for anything above $35000.
Known Limitations
- This snippet does not support Total or Product price fields.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
This question is a little off topic. Feel free to respond to this comment notification and I can provide some advice.
I have a question David. I need to round to the nearest 100 when the number is 10 or higher and down to nearest 100 when less. Example 110 to 200, 143 to 200, 265 to 300, where say 409 would go to 400.
Hm, I can’t think of a way to easily implement that with this snippet. I would take a look at the JS used by this snippet and then roll a custom solution. Wish I had a more helpful answer.
Ok I will look into that. Thx.
Hi David,
Thank you for the code – it has been a great help for me!
Can I check if I am able to round up my decimal numbers? For example, if I have “0.45”, can I round up to the whole number “1”? If I have “0.8”, can I round up to the whole number “1”?
Cheers!
Is this in a user-entered field or a calculation-based field?
Hi David,
It is a calculation based field.
Just add 0.5 to your calculation and set the rounding to 0 (screenshot).
Hi there –
The code doesn’t seem to be working on my form http://learn.innersculpture.com/test-confirmations/
Is there any way you can tell what the problem might be?
Thanks so much! Rosi
Hi Rose, it doesn’t look like the snippet has been installed correctly. It’s not loading on the page. This might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
My use case it to calculate a Security Deposit amount based on an entered Rent amount for an online rental application. When user enters advertised rent of, for example, $2,495, I want to display the required deposit, rounded down from rent to nearest 1,000 at last $200 below Rent (ignore 10s).
So,… Rent $2,495 = $2,200 Deposit Rent $1,500 = $1,300 Deposit Rent $1,495 = $1,200 Deposit Rent $3,095 = $$2,800 Deposit
Before I install the code and start messing with it, will it do this?
Love your tools!
Steve
Hi Steve, your use case is not covered by this snippet. You might be able to accomplish this with a couple Calculation-enabled Number field. Here’s a step-by-step of what I’m thinking…
( {Rent} / 100 ) - 2.5
(replace{Rent}
with your Rent field).{Base} * 100
.Assuming the rent is $2,495, the first calculation would give us “22”. The second calculation would give us “2200”. If the rent were 3095, the first calculation would give us “28”, the second calculation would give us “2800”.
( )
Thank David, I’ll give that a try. Steve
Hi David,
This snippet is almost exactly what I need. I’m using this to calculate the number of 4’x4′ panels are required to cover a wall. I need to have a multi-step formula that rounds input values along the way. Unfortunately when I create calculations that use rounded values as inputs it uses the original non-rounded values instead of the rounded ones. Is there a way to use rounded outputs as inputs for calculations?
Hi Jacob, I’m not experiencing this issue. The Calculation field correctly uses the rounded values to make the calculation. If you’d like me to take a deeper look at this issue for you, I am available for paid support. Get in touch.
Hi David,
I emailed you through ounceoftalent twice but didn’t hear back. I’d like to hire you for some custom development but the project is on a tight deadline so I’d appreciate hearing back from you asap.
Thanks,
Jacob
It works! Thanks David.
Awesome, Yi! I’m glad this worked for you.
Thanks for the great snippet, David. Getting some fun use out of it. I have a client who’s asked if I could highlight the field when the rounding is done (flash the border in red, for example) so that they recognize a change has been made. I imagine it’s a simple thing to do in JavaScript, but I know next to nothing about pulling it off. Can you provide a clue?
A “cheap” way to accomplish this is to bind a function to the change event on the field that will be rounded. You can then set a border and then clear it after x seconds. Here’s a rough example to get you started: http://pastie.org/private/vrpmoyacak46mzzqhbernw
For those wanting to round to decimals, change the instances of “parseInt” to “parseFloat” and “intval” to “floatval”. Then you’ll also have to edit the value of the “interval” variable. If you only have one value you want to round to (say rounding up by 0.5) then you can hard-code 0.5 as the interval.
Great solution, but unfortunately it doesn’t work to round up to the nearest 1. Seems like any increment other than 1 works fine.
Hi Andrew, this doesn’t handle decimals currently. It will still round to 1 but it operates as a floor rather than rounding up or down.
Yes, I was hoping to round to the nearest whole number, but “gw-round-up-1” doesn’t seem to do anything. 1.3 still rounds to 1 instead of 2.
Does it always handle decimals as a floor? That is, is 1.9999 rounded to 1? If so I could fudge it and add 0.99999 to the result prior to rounding since I want it to round up to the nearest number in all cases.
It doesn’t handle the decimal at all. It gets the integer value which is equivalent of flooring the value. Unfortunately, this means there is no fudging it. 1.99999 would still be 1.
Hiya. I’ve not tried this yet and plan to try it tonight. – If I want to use this as a rounding tool. IE: I don’t allow decimals in a field. Would this work?
So I enter 1.34 I get 1? I enter 2.99 I get 2?
Is this how it would work?
Hi! Gravity Forms 1.9.4 seems to have rendered the snippet inoperable. Going from one page of my form to the next seems to wipe whatever entry I’ve made in the field with the rounding CSS in it. Any ideas? Thanks in advance!
I’ve just tested on the demo and on my local install (with pages) and this snippet is working for me. It would seem that something specific to your install is conflicting. We do offer a $99 installation service which covers resolving simple conflicts.
great code! but is there any chance to round by 0.5?
for example 8.42 to 8.50 or 9.12 to 9.50 and so on…?
would be awesome :)
This isn’t currently possible. Are you interested in commissioning this enhancement? If so, get in touch.
Does this snippet work for rounding to the nearest 1/2 or whole number. For example, when we quote emblems, the formula is (Height + Width)/2 = Emblem Size, so if that Total Emblem Size comes out to 3.25″, we round it up to be a 3.5″ emblem.
Here is the form, we currently have everything rounding up to the next whole number.
http://theemblemfactory.com/quote-request/
Hey ric. Unfortunately, no, this snippet only supports rounding to integers currently.
Hi David, your snippet looks awesome but I am not sure it does what I need so I am wondering if there is a way to change it.
I need to round up to the nearest integer my number field, if the result of the calculation is 42.2, it should be 43, if 67.4, it should be 68.
The rounding option in GF doesn’t help as it round down below .5.
Thanks for your feedback.
Hi Olivier, since you’re working with a calculated number, I would recommend just adding 0.4. Example:
42.2 + 0.4 = 42.6; rounds up to 43
67.4 + 0.4 = 67.8; rounds up to 68
67.9 + 0.4 = 68.3; rounds down to 68
67.0 + 0.4 = 67.4; rounds down to 67
Wow David, it’s almost too simple, thanks for your help!
My pleasure!
Hi David, I used your snippet to make sure every number input has no decimals as Gravity forms does not have an integer field (only rounding with calculations).
And it worked like a charm with gw-round-1.
Thanks a lot! Robin
Thanks for sharing, Robin! Glad you found this useful. :)
David,
Will this work when the field is set to “Currency”? I can’t seem to get it to round down to increment of “9” Using: gw-round-down-9 This for a number field, where I am doing calculations for fare pricing…. But we ultimately want to get prices (after calculations) to round down to the nearest “9” in the “dollar” field.. I.E> $43.45 would become “$39” I’m not seeing any rounding if I have the number format set to “Currency”.
To see an example on the test form I’m currently working on, go to link listed in your “website” field and then enter a “One-Way Distance” value greater than 4, and look at the “Read Only (another great PERK!) field on the right labeled “OW Distance Fees”.
Thanks again for such great snippets, perks, etc. Love Gravity Wiz!!!!
Ok so, after sending the previous, I realized the snippet wasn’t installed….. Oops, I had tried it briefly a week or so back, and it caused error (not listed in wp-debug) the resulted in form not displaying. So I had removed it.
If you have already looked into the fact that this will work for number fields with calculation and formatted as a “currency”, and that it’s supposed to be working with that, I’ll send over my code, etc so we can troubleshoot it.
If this snippet doesn’t currently support that, then my next question is, “are you planning for it to do so?”
Thanks again
Ron
Your snippet looks like something i am looking for. What I can’t figure out is what I should fill in if I want the numbers to be whole numbers, so 1.5 should be 2 and 2.5 should be 3 etc
Hey Karin, this snippet will automatically round up or down to whole numbers. If you mean how to do this without this snippet, Number fields with Gravity Forms having a “Round” setting where you can indicate how many decimals to round to.
Hi David Thanks for your answer.Made me set on the right trail. Formula is working now, without the snippet.
Hello David,
I read your original comment to Karin, that this snippet automatically rounds up or down. Pardon my dumminess but what class would I use to do that?
I am using Total field not Number field. So how could I turn 198.03 to 198 or 209.96 to 210? I don’t want anything after the decimal.
Hi,
Can you help me to edit this functions so it only rounds up amounts below 10 to 10
Hi Jason, just add
gw-round-min-10
to your “CSS Class Name” setting. :)Hi David, would just like to say thanks for publishing this brilliant snippet on your site. Works like a charm :) The only thing that would improve it in my opinion is if it had a functionality to adapt to significant figures as well as increments.
Glad you found it useful! Could you elaborate on significant figures?
Sure! I have a few calculation fields in my form. The result of these calculations could be as much as £747,256 for example, or it could be as little as £756. I’m based in the UK where we have a financial regulator who states that the result of these calculations should be rounded down to 3 significant figures., so for example £747,256 should be £747,000 and £756 should be £700. At the moment, I’m using the class ‘gw-round-down-1000’. Applying an increment of 1000 to get the first number to 3 sf. however would mean that the second number comes out as 0, and I’m restricted to rounding down. My situation is probably quite a unique one, so I doubt you’ve had many other people mention this. An extra class to round up or down by significant figures would be ideal, however.
Thanks for the feedback, Catherine. If this gets requested even a few more times, I will look into adding it. If you’d like to commission me to add it, feel free to get in touch to discuss.
Thanks David :) may well be in touch in future.
Hi David,
Thank you so much for this info!
The code for the functions.php does not appear to be working properly on my development site and causes a ‘white screen’. Is there a tag possibly missing?
My apologies… just saw your video regarding this and all is well :)
Awesome! Glad the video clarified. :)