math.ceil([field]/100)*100
Solved! Go to Solution.
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")
round([field], 2)
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?
int(round([field], -2))
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))
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))
rndValue = "int(round(!ELEVATION!, -2))" ... arcpy.CalculateField_management(elev, fieldName, rndValue, "PYTHON")
That method works fine for me. Are you sure your field is the right type (long int)?
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")
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")