ArcGIS 10.2 - Proper syntax in Python to print integer?

2704
2
Jump to solution
11-25-2015 11:51 PM
VincentLaunstorfer
Occasional Contributor III

Hi,

It looks perhaps simple, but I am looking for the proper syntax in Python in order to print integer field correctly when running a script.

For the time being, I get:

ID:1.0000080689e+11

with:

gp.AddMessage("ID:" + str(input_text))

And I would prefer the proper integer value... What would be the syntax to format it correctly?

Thanks

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

I highly recommend getting familiar with Python string formatting. Try this:

id = 1.0000080689e+11

# gives you the same result

gp.AddMessage("ID: {}".format(id)) 

# will display as a number with no decimals

gp.AddMessage("ID: {:.0f}".format(id))

# or show off and add commas

gp.AddMessage("ID: {:,.0f}".format(id))

Unless you have a really good reason to use gp (for example, you want to write code that is backwards-compatible to 9.3), I highly recommend using arcpy instead.

View solution in original post

2 Replies
curtvprice
MVP Esteemed Contributor

I highly recommend getting familiar with Python string formatting. Try this:

id = 1.0000080689e+11

# gives you the same result

gp.AddMessage("ID: {}".format(id)) 

# will display as a number with no decimals

gp.AddMessage("ID: {:.0f}".format(id))

# or show off and add commas

gp.AddMessage("ID: {:,.0f}".format(id))

Unless you have a really good reason to use gp (for example, you want to write code that is backwards-compatible to 9.3), I highly recommend using arcpy instead.

View solution in original post

VincentLaunstorfer
Occasional Contributor III

Thanks. It works very well. As long as variable id is a number (integer or float) and not a string.

Also, I used the former 9.3 syntax because this script was initially made at that time and I haven't upgraded the code to arcpy...