Hi Everyone,
I’m trying to round off list of floats to two decimal places. It’s hard to do it according to normal dynamo node Round. Below, You can find my intention.
There is a good python formula for 2 decimal places (above ). The problem is - how to put this formula to work correct with groupped list ( to get result like on 1st pic ). Probably “for” loop should work.
# Charger les bibliothèques DesignScript et Standard Python
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
dataEnteringNode = IN
x=IN[0]
a=[]
# Placer votre code au-dessous de cette ligne
for i in x:
p="%0.2f"%(round(i,2))
a.append(p)
# Affectez la sortie à la variable OUT.
OUT = a
This probably isn’t the most efficient way, but this will work.
rounded_list = []
for list in IN[0]:
temp_list = []
for item in list:
rounded = "%.2f" % round(item, 2)
temp_list.append(rounded)
rounded_list.append(temp_list)
Edit:
You could also write it in list comprehension form. [["%.2f" % round(item,2) for item in list] for list in IN[0]]
Yeah, this solution assumes you’re lists never go more than a single layer deep.
If you know nesting depth going to be consistent you could add another for layer/bracket until you get the depth you need. If you want it to be more flexible then you’d need to put in some more logic using (a) if statement(s). In fact, at that point you might want to make a recursive function for it.
Had a little bit of time to kill, so I started messing around with a more flexible idea. Unfortunately, not enough time to finish it lol
But maybe it’s a starting point
def nested_round(list):
out_list = []
for obj in list:
tmp_list = []
if isinstance(obj, list):
nested_round(list)
else:
rounded = "%.2f" % round(obj, 2)
tmp_list.append(rounded)
out_list.append(tmp_list)
return out_list
Hello,
If you have to send your data to a parameter in number format despite the rounding (it keeps the decimal parts, is there a node to only manage the display of decimals before sending)
I did not know that by using the display with 2 decimal places under python (this transforms into a text string)
Stewart- thanks for help. It works. I just pyt “OUT = rounded_list” in the end of code.
Finally, the problem is in differences between Revit/ Dynamo rounding off.
Please find attached example.
Revit in rounding 4.2549 to 4.255 (3dp)
Dynamo is rounding to 4.25 (2dp)
To get the 3dp Revit value to 2dp you’re double rounding which will always introduce some degree of inaccuracy. If you change your project units for areas in Revit to be 2dp, then you’ll see the same 4.25 value inside of Revit as they’re both rounding from the same initial value to the same about of decimal places.