Revit. Dynamo + Python Script / Round off to two decimal places

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.

Regards,
Kamil

Hello, here is a partial answer
as I am a quiche under python, and I do not know how to manage nested lists
I flattened before


code:

# 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

Cordially
christian.stan

1 Like

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]]

3 Likes

hello and thank you, that’s nice of you
depth list 2 (2 for)
You must apply an if, if you do not know the depth level of the list

Cordially
christian.stan

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.

1 Like

Thank you for your comprehensive explanations. :ok_hand:
Sorry mr. kamil to have a little squatted :wink: your topic

Cordially
christian.stan

Don’t run this it’ll freeze

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
1 Like

you could make it a node and just apply list leveling to it

Any chance that you’re using the ‘round’ node without the digits overload?

3 Likes

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)

Capture d’écran 2022-10-29 113414

Cordially
christian.stan

1 Like

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.

Those aren’t differences.

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.

Hi,
Is anyone know why Revit ( in schedule ) rounds of in this strange way?

image