Select to view content in your preferred language

# Covert double to float

604
4
10-26-2022 07:24 PM
by
New Contributor II

I got a field A which is double, but it only has 6 decimal points. I want to turn the field type to float

so i did the following

using arcpy.management.AddField to create a field B with float type

use arcpy.management.CalculateField to copy the value from field A to field B

but the problem is after this conversion, I use arcpy.SearchCursor to retrieve the value of field A and B and check if they are the same, it turns out field B value's decimal point is more than field A

fieldA : [0.046486, -0.466769, 0.562891, 0.783896, -0.837999, -1.58879, 2.39555, 1.38319, 1.93933, 1.48517, 1.73189, 0.723564, 0.414805, -0.014135, ...]

fieldB: [0.04648600146174431, -0.4667690098285675, 0.5628910064697266, 0.7838960289955139, -0.8379989862442017, -1.5887900590896606, 2.395550012588501, 1.3831900358200073, 1.939329981803894, 1.4851700067520142, 1.7318899631500244, 0.7235640287399292, 0.41480499505996704, -0.014135000295937061, ...]

but this only happens if I inspect the field B value in python, in ArcGISPro, they are the same in the table

how may I make should that they will be the same even in Python? Thank you

4 Replies
MVP Frequent Contributor

This sounds like you have to increase the nummber of displayed decimals for the field.

Format numeric and date fields—ArcGIS Pro | Documentation

Have a great day!
Johannes
by
New Contributor II

The display in the ArcGISPro table does not show the extra decimal point. In fact, they are the desired values. They are the same as the original double field, it is what I expect to see when I inspect the float field with arcpy.da.SearchCursor.

I tried the following ways in calculating the value in the float field

1. using arcpy.da.UpdateCursor

``````with arcpy.da.UpdateCursor(in_table = feat_cls_fp, field_names = [field_name, new_field_name]) as cursor:
for row in cursor:
row[1] = row[0]
cursor.updateRow(row)``````

2. Using arcpy.management.CalculateField

``````arcpy.management.CalculateField(
in_table = feat_cls_fp,
field = new_field_name,
field_type = new_field_type,
expression=f"!{field_name}!",
expression_type="PYTHON3",
enforce_domains="NO_ENFORCE_DOMAINS"
)``````

In the above code, The new_field_name is the float field name, the field_name is the double field name.

and I check their consistency by the following custom function, which is basically using arcpy.da.SearchCursor

``````def get_field_values(cls, feat_cls_fp: str, field_name:str) -> list:
table = arcpy.da.SearchCursor(
in_table = feat_cls_fp,
field_names = field_name,
spatial_reference = arcpy.Describe(feat_cls_fp).spatialReference
)
return [row[0] for row in table]

field_values = get_field_values(feat_cls_fp, field_name)
new_field_values = get_field_values(feat_cls_fp, new_field_name)
assert field_values == new_field_values, f'Field {field_name} values changed after changing data type.'``````

but none of them produces the desired output and passes the assertion check.

the field_values are the same with the op fieldA value, which is the original double values

the new_field_values are the same with the op fieldB value, which is the converted float value, which has the extra decimal point after update/calculation.

I have also tried to create a new field with arcpy.management.AddField, specifying the field type as 'FLOAT' and the field_precision and field_scale as 6. but it does not suppress the extra decimal point either.

MVP Esteemed Contributor

arcpy.SearchCursor is deprecated use

arcpy.da.SearchCursor

SearchCursor—ArcGIS Pro | Documentation

And did you specify a spatial reference when using it? you might be getting single precision returns if you didn't.  It is worth a check if indeed your inputs only have 6 decimal places to begin with

... sort of retired...
by
New Contributor II

I use the same SearchCursor to inscept the double Field which has only 6 decimal point values return. Even i provided the spatial reference, the float Field value's decimal point is still inconsistent with the double Field

my search cursor is define as the following

``````def get_field_values(cls, feat_cls_fp: str, field_name:str) -> list:
table = arcpy.da.SearchCursor(
in_table = feat_cls_fp,
field_names = field_name,
spatial_reference = arcpy.Describe(feat_cls_fp).spatialReference
)
return [row[0] for row in table]``````