Converting int to string adds a decimal with python?

Hey Guys,

Ive been trying to start to learn python… It’s going fantastic :sweat_smile:

Im trying to convert ints to strings in a list, without using existing nodes, i wanted to try to write as much in Python, just to get the hang of Python.
When i convert it to a string, a decimal gets added and i’m not sure why…
image

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
a = [round(num,0) for num in IN[0]]
b = [str(x) for x in [round(num,0) for num in IN[1]]]
OUT = a,b

I am not sure how to handle this, could anyone help me out with this?
When i try this in powershell it works as i intend:
image

Rounding doesn’t change the object type. You have to actually convert the double to an int. This goes for Dynamo or Python.

2 Likes
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
a = [round(num,0) for num in IN[0]]
b = [str(int(round(x))) for x in IN[1]]

OUT = a,b

Capture d’écran 2022-08-19 195702

2 Likes

Hmm interesting,

Out of curiosity, why does powershell handle the action as i want it to? or am i not feeding doubles in that example?

Different language. Different rules for handling objects.

1 Like

in this particular case, python’s built in round function does not return an int - powershell’s probably does.

2 Likes

Yeh i was wondering about this, since i was using Python in the powershell window :thinking:

it also looks like you are using ironPython here, so there could be differences in the implementation there as well, or some marshaling going on between ironPython and Dynamo.

In a newer version of Dynamo you can try Cpython3 and see what you get - it’s important to compare apples to apples (ie the version and implementation of Python)

2 Likes

I had tried at the same time, you can grant the solution to Mr. Nick (He gave you, the reason, not me)

Hello, if the slight digression is possible
the type is not returned?

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
a = [round(num,0) for num in IN[0]]
b = [str(int(round(x))) for x in IN[1]]
c=type(a)
d=type(b)
e=type(a[0])
f=type(b[0])

OUT = a,b,c,d,e,f

Capture d’écran 2022-08-19 200831

Cordially
christian.stan

edit:
I’m really stupid, sorry, Mr. Nick had already given the answer above, sorry
Capture d’écran 2022-08-19 202255

1 Like

To add to the options discussed here, in Python you can use the rstrip method with “.” to split. Then index the result at index 0. For unsplit strings you will get the whole outcome and for those that got split you will get the leading portion.

1 Like

A solution using format strings (Python2 and 3) or f-string (Python3)

3 Likes