Select to view content in your preferred language

Round to nearest 100th

10539
15
Jump to solution
06-06-2013 07:57 AM
ChrisBrannin
Regular Contributor
I am a little confused about how to round a field to the nearest 100th. Would I use math.ceil() or the round()?

math.ceil([field]/100)*100


Does this only round down though?

Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor
Deleted my last post after re-reading.

However, it looks like you can use one for loop to replace all three that you are running.

might try something like:


import arcpy import os, math from arcpy import env   in_workspace = r"F:/Inventory_export_dev/testRnd" fcList = []    for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):     for filename in filenames:         arcpy.AddField_management(os.path.join(dirpath, filename), fieldName, "SHORT", "", "", "", "", "NULLABLE")         arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON") 


No need to put the filenames in a list if all you are going to do is iterate through them again.  Just as well do the work on the first iteration.

Of course, this will errror out if any of the FC's in your in_workspace do not have the numeric ELEVATION field.

R_

View solution in original post

0 Kudos
15 Replies
MathewCoyle
Honored Contributor
math.ceil() always rounds up. math.floor() rounds down. round() rounds the the nearest integer or to a particular decimal place if the second parameter is specified.
round([field], 2)
0 Kudos
ChrisBrannin
Regular Contributor
I am a little confused about how to round a field to the nearest 100th. Would I use math.ceil() or the round()?

math.ceil([field]/100)*100


Does this only round down though?

Thanks


How I mean to phrase this question is, how to round a number (say 11) to 0 instead of 100?
0 Kudos
MathewCoyle
Honored Contributor
How I mean to phrase this question is, how to round a number (say 11) to 0 instead of 100?


You would use a negative number for the second round() parameter of course. Use int() to ensure it returns an integer instead of float.

int(round([field], -2))
0 Kudos
ChrisBrannin
Regular Contributor
You would use a negative number for the second round() parameter of course. Use int() to ensure it returns an integer instead of float.

int(round([field], -2))


That is the winner right there, thank you! I knew ther was a better way then to use math.ceil(). I really appreciate the help.
0 Kudos
ChrisBrannin
Regular Contributor
You would use a negative number for the second round() parameter of course. Use int() to ensure it returns an integer instead of float.

int(round([field], -2))


How can I use this outside of arcmap? When I try, an error of "Invalid field [field]" appears.

I am trying to use it like this:
rndValue = "int(round(!ELEVATION!, -2))"

...

arcpy.CalculateField_management(elev, fieldName, rndValue, "PYTHON")


Thank you
0 Kudos
MathewCoyle
Honored Contributor
That method works fine for me. Are you sure your field is the right type (long int)?
0 Kudos
ChrisBrannin
Regular Contributor
That method works fine for me. Are you sure your field is the right type (long int)?


I had no issue with it in arcmap attribute table field calculator. Just in IDLE... My code below:

import arcpy
import os, math
from arcpy import env


in_workspace = r"F:/Inventory_export_dev/testRnd"
rndValue = "int(round(!ELEVATION!, -2))"
fcList = []
fieldName = "SL_ELEV"


for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):
    for filename in filenames:
        fcList.append(os.path.join(dirpath, filename))


for list in fcList:
    arcpy.AddField_management(list, fieldName, "SHORT", "", "", "", "", "NULLABLE")


print "Rounding ELEVATION field to SL_ELEV"


#Rounds values from [ELEVATION] to [SL_ELEV]
for elev in fcList:
    arcpy.CalculateField_management(elev, fieldName, rndValue, "PYTHON")


am I missing something?
0 Kudos
MathewCoyle
Honored Contributor
I would try printing out your elev value for each iteration of fcList to make sure it is what you expect.
0 Kudos
RhettZufelt
MVP Notable Contributor
Looks like you are trying to round the ELEVATION variable before you have set it to anything.

Need to either set it or extract it from the attributes first.

Also, both your for loops are working the same data, in one you assign it to list, in the other you assign to elev.  But, in both cases, you are still just itering through the values in fcList, so just as well do it in one swoop.

Think you are looking for something like this:

import arcpy
import os, math
from arcpy import env


in_workspace = r"F:/Inventory_export_dev/testRnd"
fcList = []



for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):
    for filename in filenames:
        fcList.append(os.path.join(dirpath, filename))


for list in fcList:
    arcpy.AddField_management(list, fieldName, "SHORT", "", "", "", "", "NULLABLE")
    arcpy.CalculateField_management(list, "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")



Haven't tested, but should go through each FC in your fcList add the field and populate it with the round value.

R_
0 Kudos