Springs.Number.ToString

Can anyone give me a design script solution for this…

If you just want to get integers you can connect a Math.Floor node and Object.ToString. The rounding feature probably wouldn’t be too difficult but I don’t have Dynamo right now to come up with a solution.

You can only call ZeroTouch (C# nodes) inside of DesignScript currently (So not Python or Custom Nodes).

You can achieve your desired result very quickly in Python though in one line :slight_smile:!

What this is doing in Python is the following:

  1. Use a List Comprehension to run a For loop inside a singular line
  2. Make each number in your list an Integer first (Whole number) if it can
  3. Then make that whole number into a String for each individual item
  4. Then return the list of values, in their correct order, out of the Python node
OUT = [str(int(i)) for i in IN[0]]
6 Likes

image

Perfect, thanks!

1 Like

@cgartland

I didn’t test this properly, the math floor function returns an integer so I’m back to the Springs.Number.ToString node…

Can you explain what you are looking for? It is not really clear what your end result is supposed to be.

If you need to nearest x decimal, there is Math.Round where you can give it how many digits. If you need the floor equivalent for decimal places, you can *10 and then /10 or 100, etc. There is also the normal String from Object node

I want to convert a decimal number to a text string - see above. The Springs node works for me but I wanted to embed in the design script to clean up my code.

And why was the above version with math floor not working? Once you added the “Test”+b part, it becomes a string.

It converts to integer so I lose the decimal component, as shown

Then you can use Math.Round and select the amount of digits. Or do what I said above where you can *10, math.floor, and then /10 or whatever amount of decimals you want.

Once you do the c = "Test" + b; any integer or float will be automatically changed into a string.

Please at least try it out and then if it still isn’t working, show what you want as exact as you can, because the first post looks like you wanted to drop all of the decimal places.

I’m looking for a design script solution that gives me the same results as the Springs node exactly as shown below with the input being a decimal number, ie no trailing zeros…

1 Like

So the Springs node uses a function called unicode which is a much better exact translator than string from object or other methods Dynamo has.

I made this version in design script and it seems to work for now. You can change how many decimal points to round to in the first line. I have it set to 3:

springsround

rd = Math.Round(num, 3);
st = "Test " + rd;
t0 = String.Split(st, ".");
t1 = List.GetItemAtIndex(t0@L2<1>, 1);
t2 = List.GetItemAtIndex(t0@L2<1>, 0);
t3 = String.Replace(t1, "0", " ");
t4 = String.TrimTrailingWhitespace(t3);
t5 = t4 == ""? t4:String.Replace(t4, " ", "0");
t6 = t5 == ""? t5:String.Insert(t5, 0, ".");
t7 = String.Join("",List.Transpose([t2, t6]));

Here’s a viable solution in python as well:

def format_num(n):
    if int(n) - n == 0:
        return str(int(n))
    else:
        return '{:.2f}'.format(n)

a = 1
b = 1.0000000
c = 1.9000001

nums = [a, b, c]

OUT = [format_num(num) for num in nums]

I should also note that this will return ints if that is what is provided and will otherwise return a float truncated to 2 decimal places.

EDIT: See later post which removes trailing zeros instead of truncating the number.

Wow, nice programming, I’ll have a look and test over the weekend. I’m not quite ready to make the leap to Python but that’s the logical next step. I’m not a programmer by background so it’s quite hard for me.

If you just wanted the python, you could copy and paste what is in the springs node as it is all in python using the unicode command. I thought you were specifically asking for design script.

Sorry, I was testing that out and there was a couple problems. First, the call is wrong, in the OUT you have an s at the end of the function name but the function doesn’t.

Second, he asked for without trailing 0’s, so the rounding with the format to nearest decimal doesn’t work if there is a 0 in that decimal place. See below:

springsround_cgartland

Kenny, good catch. I wrote my post around 12:30am local time—not exactly the best time to be doing these things. See below a revised method of removing trailing zeros:

import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
import re

def format_num(n):
    if int(n) - n == 0:
        return str(int(n))
    else:
        return re.sub('0+$', '', str(n))

a = 1
b = 1.0000000
c = 1.9000001
d = 1.200304000
nums = [a, b, c, d]
OUT = [format_num(num) for num in nums]

This method uses a simple regex pattern that matches one or more zeros (0+) followed by the end of a line ($).

4 Likes