Numbers to Strings

I often run into the situation in Dynamo that I need to convert numbers to strings. Most of the time I end up with strings containing many decimals like .000000

Of course I can fix that with dynamo nodes but one python script node is handier. The only thing is I can’t get the system locale working atm.

# Enable Python support and load DesignScript library
import clr
from Autodesk.DesignScript.Geometry import *

import locale
langlocale = locale.getdefaultlocale()[0]
locale.setlocale(locale.LC_ALL, langlocale)
dp = locale.localeconv()[‘decimal_point’]

if isinstance(IN[0], list):
    numbers = (IN[0])
    numbers = [(IN[0])]

output = []

for n in numbers:
    tmp = str(n)
    # You might have to change the splitting character to work with your system regional settings
    splist = tmp.split(“.”)[0] # now the splitter is hard coded
    # splist = tmp.split(dp)[0] only use this line if locale is working     
    # split generates two items in a list this one gets the first

OUT = output

Numbers to Strings

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: