Arcpy Calculate Field Stores a String in Scientific Notation

2427
13
12-07-2018 11:44 AM
BrandonZaitz
New Contributor III

I have an attribute field that is API well numbers and when I calculate a "TEXT" field using arcpy it converts those API numbers into scientific notation. However, when I calculate the field in Arcmap using the GUI it stores the full API number correctly. Has anyone else had this issue? How might i ensure that this doesn't happen? 

I have tried both:

arcpy.CalculateField_management(shapefile,fieldname,"!{}!".format(inputfieldname),"PYTHON")

and:

arcpy.CalculateField_management(shapefile,fieldname,"str(!{}!)".format(inputfieldname),"PYTHON")

0 Kudos
13 Replies
JoshuaBixby
MVP Esteemed Contributor

Can you give an example number?

0 Kudos
BrandonZaitz
New Contributor III

For instance, "30015364700000" will store as "3.00153647e+13".

0 Kudos
DanPatterson_Retired
MVP Emeritus

The field width is > 14 characters and defined as Text for the output field?

The only other thing I can think of is explicitly formatting the output string

a = 30015364700000

"{:<14.0f}".format(a)
BrandonZaitz
New Contributor III

The field width is 255 and is a string. The code you provided only works if i change the 'f' to 's'.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I just tried using your code with both a feature class and shape file, and it works correctly for me, i.e, it doesn't use scientific notation.  What version of ArcMap are you using?

0 Kudos
BrandonZaitz
New Contributor III

Arc Version is 10.6.0.8321

0 Kudos
DanPatterson_Retired
MVP Emeritus

If 'Long' in ArcMap or ArcGIS Pro is 32bit integers, then the maximum length of a field and the maximum value is dictated by the integer type.  From other c based number types, these are as follows

32bit integers 10 characters with the maximum as 2,147,483,647

Arc* stores long as -2,147,483,648 to 2,147,483,647

64bit integers 19 characters with the maximum as 9,223,372,036,854,775,807

your number is 14 characters long as an integer, 30,015,364,700,000 .... beyond the 32bit long maximum 

So if your key has to be that long, then you had better use a text field since 

np.iinfo(np.int32).max
2147483647

np.iinfo(np.int64).max
9223372036854775807‍‍‍‍‍

Now if we convert that integer you have to float32 (32 bit float) we get

np.asarray([30015364700000]).astype(np.float32)
array([3.0015364e+13], dtype=float32)

np.asscalar(a)  # not good
30015364268032.0
b =np.asarray([30015364700000]).astype(np.float64)
array([3.00153647e+13])
np.asscalar(b)  # better
30015364700000.0
JoshuaBixby
MVP Esteemed Contributor

I had to store the number as double and then use the code to covert to string.  As you point out, the number can't be stored in long.

0 Kudos
BrandonZaitz
New Contributor III

The field i am calculating from is a Double.

0 Kudos