Problem with script tool parameters (parameter comes in as string when I want float)

1189
13
Jump to solution
06-10-2014 08:37 AM
AmelieDavis
New Contributor II
Hi!

I have a python script that runs fine stand alone but when I try to make it GUI based by adding it to a toolbox as a script I get errors which I believe are related to how I am setting the parameters.

I have three values that I need to be set as a real number with a decimal but they keep coming in as string.

In ArcGIS toolbox, under properties of the model, and the parameters tab, I set those three parameter types to Double (I have also tried String but that doesn't work either).

then in python, that part of the code looks like this:
# -*- coding: utf-8 -*-  ############################################################################  import sys # Import arcpy module import arcpy #from arcpy import env  # Set this to False once testing is complete arcpy.env.overwriteOutput = True  ############################################################################     # Script arguments  # user defined weights for importance of up to three floral seasons Spring = float(arcpy.GetParameter(3)) arcpy.AddMessage("Floral Weight #1 is equal to: " + str(Spring)) if Spring == '#' or not Spring:     Spring = 0.3 # provide a default value if unspecified check1 = arcpy.Parameter(Spring) arcpy.AddMessage("Data type is: " + str(check1.datatype))  Summer = float(arcpy.GetParameter(4)) if Summer == '#' or not Summer:     Summer = 0.6 # provide a default value if unspecified      Fall = float(arcpy.GetParameter(5)) if Fall == '#' or not Fall:     Fall = 0.1 # provide a default value if unspecified      Tot = Spring + Summer + Fall arcpy.AddMessage("Floral Weights are equal to: " + str(Tot)) if Tot == 1:     arcpy.AddMessage("Sum of floral weights is equal to: " + str(Tot)) else:     arcpy.AddError("The sum of floral weights must equal to one.")     sys.exit() # exit out of model if weights don't add up to 1  ############################################################################


I've tried GetParameter and GetParameterAsText but the value always seems to be coming in as a string.

I know this is probably something very basic but I just can't figure it out.

I'm using ArcGIS 10.2.

Thank you in advance for any assistance.

Sincerely,
Amelie
Tags (2)
0 Kudos
13 Replies
AmelieDavis
New Contributor II
Must be some setting on my computer. 

Still get the same problem:

Executing: Script1 0.6 0.3 0.1
Start Time: Tue Jun 10 17:00:52 2014
Running script Script1...
1
  all NOT good
Completed script Script1...
Succeeded at Tue Jun 10 17:00:52 2014 (Elapsed Time: 0.01 seconds)

These are the only lines of code:
import arcpy

a = arcpy.GetParameter(0)
b = arcpy.GetParameter(1)
c = arcpy.GetParameter(2)

total = a + b + c
arcpy.AddMessage(total)
if total == 1.0:
    arcpy.AddMessage("  all good")
else:
    arcpy.AddMessage("  all NOT good")


I only have three parameters and they are all set up to Double.

yet the 0.6, 0.4, 0.1 combo doesn't work

it works in yours?
0 Kudos
AmelieDavis
New Contributor II
OMG!!

Check this out:
http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html


Armed with this knowledge, I changed the code to this:

import arcpy

a = arcpy.GetParameter(0)
b = arcpy.GetParameter(1)
c = arcpy.GetParameter(2)

total = ((a * 100) + (b * 100) + (c * 100))
arcpy.AddMessage(total)
if total == 100:
    arcpy.AddMessage("  all good")
else:
    arcpy.AddMessage("  all NOT good")


And now it works!!! yay!

My hair stylist thanks you.
No, seriously though, THANK YOU for helping me work this out.  I thought I was going crazy.

Amelie
0 Kudos
AdamCox1
Occasional Contributor II
OMG is right, that is fascinating.  I was just finding that same issue, also with .7, .2, .1, and also running it as a stand alone script.  Was thinking of how it's weird that it's only a problem when .1 is the last digit, and when there is only one .1 value.  Was thinking we may need to turn to stack overflow...

Well, nice work, and that looks like a good work-around.

Cheers!
0 Kudos
AmelieDavis
New Contributor II
🙂  Thanks again.
0 Kudos