Reduce decimal digits of numbers in a list


#1

Hi,

I’m trying to reduce the decimal digits to two of the numbers that I have in a list, for example, 98.9987 should be reduced to 98.99. I made the simple script in the image below but I got a runtime error. Any idea? It seems puzzling considering that the script is very simple.


(David Rutten) #2

98.9987 -> 98.99 is a string operation, since it doesn’t follow rounding logic (should be 99.00 mathematically speaking). This means you either have to just chop off the digit characters from a textual representation of the number, or you can multiply your number by 100, round to the nearest lower integer, then divide by 100 again. That might be easier in python.


(David Rutten) #3

Also note that any data that flows through a panel becomes text. So you are not in fact feeding numbers into your script, but strings.


Reduce decimal without rounded
#4

Interesting! I didn’t know that any data becomes text. I failed to explain that I did the same operation on one number and it worked (see image below), I’m wondering why this didn’t work when I input a list?


(David Rutten) #5

I think because your [x] input isn’t set to list access. You don’t have to perform the looping yourself, you can just execute the python script on your list and Grasshopper will iterate it for you. You only have to iterate over lists if your algorithm depends on the entire list (for example when computing averages, or min/max values).


#6

Aha! it was list access. Thanks a bunch!