Select to view content in your preferred language

Round up in Field Calculator

8873
3
03-14-2012 04:02 PM
ChrisGraves
Regular Contributor
Hi GIS Gurus,

I'm coming across the issue were the round function performs round to even which means 56.5 becomes 56 and 55.5 becomes 56. What I would like to do is

round for example 56.5 up to 57 using the field calculator but cannot get the syntax to work.

Any suggestions or ideas on this would be great.

Thanks guys,

Chris
0 Kudos
3 Replies
curtvprice
MVP Esteemed Contributor
I'm coming across the issue were the round function performs round to even which means 56.5 becomes 56 and 55.5 becomes 56. 


Helpful to know that the VBScript Round function does "banker's rounding" with .5 values -- up or down with the root number odds and evens so totals won't be biased up up or down.  Back in the day, I remember hearing this referred to by our streamgaging techs as "USGS Rounding".  You can see how measurements could be biased over the long run if you always rounded up when you hit the middle value.

I googled around and it turns out Python 2.x round() does not do banker's rounding, but Python 3.x round() does. (Note, Python 3.x probably won't be used by ArcGIS for a good long time, as it is a big change.)

If you really want to avoid it, an approach that should work with positive numbers (pretty consistently, independent of scripting language) would be to add half and integerize:

Int([field] + 0.5)
ChrisGraves
Regular Contributor
Thats great thankyou for your reply this has done the job!

Quick query how does this calculation calculate 49.2 back to 49? Wouldn't it add 0.5 to the number and make it 49.7 then round it up to 48?

Sorry for the obvious question.

But thanks all the same.
0 Kudos
curtvprice
MVP Esteemed Contributor
How does this calculation calculate 49.2 back to 49? 


VBScript's Int() does not round, it truncates. (49.2 + .5 = 49.7; Int(49.7) = 49.)

You're welcome!
0 Kudos