[GIS] Setting input parameter to be float for Python script tool

arcgis-10.3arcpyparameterspython-script-tool

I made a Python (2.7.8) script, where one of the parameters is height (float) and I wan to make tool in ArcGIS 10.3 now. However in data type list there is only double. If I input integer value into this parameter field (e.g. -480), the tool works fine. If i input e.g. – 480,5 i get error

Traceback (most recent call last): File "D:\HETMAN\GIS\Projekt Model
Złoża\Sktypy.py", line 27, in ValueError: invalid literal for
float(): -480,1

Failed to execute (SSKv1).

This is a fragment of code, where i try to convert value to float:

kontakt_dbl = sys.argv[3]
kontakt = float(kontakt_dbl)

How to convert double value into float and not to get error?

Best Answer

Your issue is arising due to the use of a comma as the decimal mark. It may help to understand that in your script you are not converting a double into a float, you are converting a string into a float. The string, when a comma is used as the decimal mark, is invalid.

I would make use of the Replace method here to ensure that a decimal point is used in the script:

kontakt_dbl = sys.argv[3]
kontakt = float(kontakt_dbl.replace(",","."))

In this case if a user inputs "-480,5", it is changed to "-480.5" which is a valid input.

Regarding data types, the data type Double is a floating point number. ArcGIS uses two types of floating point numbers, differentiated by precision: Float (single-precision) and Double (double-precision). The following page explains the difference in more detail: http://resources.arcgis.com/EN/HELP/MAIN/10.1/index.html#/ArcGIS_field_data_types/005s0000000p000000/