Python Number Formatting

#1

Hello everyone,

I am trying to get my list of numbers to format with a minimum of : 000.00 where this is also fine 0000.00
I found this option through python

Which looks perfect, however my execution is not there.
It seems to take my double input and convert it to a string for some reason, and my attempt to cast the input to a float didn’t work either.

Any help would be greatly appreciated.
-Thanks

#2

This should work fine–you just need to iterate your list. Your input is a list of 7 numbers, so you should format each of them individually.

input = IN[0]
types = [str(type(i)) for i in input]
formatted = ['{:06.2f}'.format(i) for i in input]
OUT = types, formatted
4 Likes
#3

Thanks that worked great.


EDIT: Side note, the python number formatting function rounds numbers too instead of just cutting off the trailing numbers :grin:

#4

If you want to cut off trailing numbers (and not round), that is the same as the floor function. Dynamo has built in nodes for that called Math.Floor but to do it in python, you have to import the math module and then call it with .floor(x) method.

Keep in mind that for both of these (dynamo node and python function) will floor to the nearest integer or whole number so you will have to first multiply by 100, floor it, then divide by 100.

It will end up being something similar to this (using @cgartland’s code as the base) :

import clr
import math
input = IN[0]
types = [str(type(i)) for i in input]
formatted = ['{:06.2f}'.format(math.floor(i*100)/100) for i in input]
OUT = types, formatted

pythonfloor

3 Likes