Select to view content in your preferred language

Rounding issues using Python in ArcGIS Pro

8077
12
Jump to solution
02-05-2018 08:38 AM
AdrianWelsh
MVP Honored Contributor

It seems to me that ArcGIS Pro is not rounding my numbers correctly when I use Python in Calculate Field. Or my expression is incorrect.

Here is my example:

The Shape_Area of a feature is 7464.72748

My Calculate Field expression is

str(int(round(round(!Shape_Area!), -1)))

I would think this should result in a string value of 7470, but it gave me 7460.

Where am I going wrong? Shouldn't round(7464.72748) give me 7465 more or less?

And then round(7465, -1) give me 7470?

0 Kudos
12 Replies
MarcoBoeringa
MVP Alum

Please also have a look at the "decimal" module of Python:

https://docs.python.org/3.6/library/decimal.html

It may offer functions you need as well. You can import the module in e.g. the Calculate Field tool.

0 Kudos
AdrianWelsh
MVP Honored Contributor

Rounding is ridiculous.

Joining up an "engineer's" spreadsheet with sorta-rounded-area numbers (integers) with a converted CAD drawing takes more effort than it should. This is what my code block Calculate Field expression had to look like to get as close as I could (though, I likely could have gotten closer with more ceilinging and flooring, but I got lazy):

import math
def ceiling(parcel, area):
    return str(parcel) + " - " + str(int(math.ceil(math.floor(area) / 10)) * 10)

__esri_field_calculator_splitter__

ceiling(!Parcel!, !Shape_Area!)

thanks, Dan, for all the assistance!

DanPatterson_Retired
MVP Emeritus

engineers???  cool!   use the base 9 thing.. that will keep them going for a few hours