3

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?

0

2 Answers 2

8

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/

3
  • 2
    You may want to use locale.atof() to let the parsing of float depend of the environment locale.
    – Cyrbil
    Nov 26, 2015 at 21:23
  • @Luke: see answer below.
    – Cyrbil
    Nov 27, 2015 at 7:13
  • 1
    Dont use locale because it depend of production process data. If this is not the same software or a same language you have got a problem because number can be stored with point or comma... In this condition, i prefer cast data because i know destination format but not source format. If it's necessary, I check with "if isinstance( v, basestring): " Nov 27, 2015 at 11:52
2

You can use the locale module to let the parsing of float depends of the environment locale.

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '') # (optional here) use user's preferred locale
>>> locale.getlocale()
('en_GB', 'ISO8859-1') # my current env locale
>>> locale.atof('12,3')
123.0 # wrongly parsed

# with another locale, parsing is correct
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
('de_DE', 'ISO8859-1')
>>> locale.atof('12,3')
12.3

Information about a local can be seen with:

>>> locale.localeconv()
{
    'mon_decimal_point': ',',
    'int_frac_digits': 2,
    'p_sep_by_space': 1,
    'frac_digits': 2,
    'thousands_sep': '.',
    'n_sign_posn': 1,
    'decimal_point': ',',
    'int_curr_symbol': 'EUR ',
    'n_cs_precedes': 0,
    'p_sign_posn': 1,
    'mon_thousands_sep': '.',
    'negative_sign': '-',
    'currency_symbol': 'EUR ',
    'n_sep_by_space': 1,
    'mon_grouping': [3, 0],
    'p_cs_precedes': 0,
    'positive_sign': '',
    'grouping': [3, 0]
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.