Jump to content
Developer Wiki and Function Reference Links ×

Rounding numbers and setting number of decimal places

Recommended Posts

  • Marionette Maven

Hey Kevin,

So rounding doesn't appear to truncate the number, which is what you're really looking for.

If you use the "trunc" node after your "round" node, I think you'll get what you want.

I'm also in the process of getting nodes to follow the rounding display preferences set in the application, but that won't be available for a bit longer.


Trunc may only solve the issue if you're looking for whole numbers.

Edited by MarissaF
Link to comment
  • Marionette Maven

Also - to follow up.

Since the reason for your rounding is display purposes, another way to get rid of trailing zeros would be to use the "String" node (str) in the String category.

This would be useful for the cases where you may want to round out 3 decimal places but still see what's past the whole number.

After you convert the number to a string, you can put it right back into the text object.

Edited by MarissaF
Link to comment

Hi Marissa,

Thanks for the info. I'll look at Trunc.

I accidentally posted an old image with my original post. Here's a clearer example of what's happening. The Layer elevation is set to 150.5mm. The top object contains the Round node, which doesn't round or truncate. The bottom node is without it showing the value it should be acting on.

The Round node is the first node I've encountered that only contains Python script. It doesn't call the Vectorscript rounding function. I looked online for some documentation and Round seems to be correct. I found this documentation that talks about getting weird results from a "Float" - https://docs.python.org/3/library/functions.html#round

Perhaps its something with the output of the Get Layer Elevation node output? I don't know what a "Float" is....



Edited by Kevin McAllister
Link to comment
  • Marionette Maven

A float is a variable type. You could compare it to a double.

Since Python is an interpreted language, it generally chooses the type of our variables on its own.

In actuality, I believe ALL of our Math nodes use only Python. This is for development reasons, Vectorscript isn't really being maintained anymore since Python is much more flexible.

The reason the number of decimal places is so long is because VW uses long floats in its math. This becomes more apparent when displaying values from a script. Since VW has the preference of how far out numbers are displayed, many users aren't aware how precise the values used in the software actually are.

In my post right above your most recent one is my best solution so far. The Str node will get rid of trailing zeros, so after you pick your precision with the Round node, it's done the best at displaying what you want (except in the case of an Integer, then you would want to use Trunc). This will also change the variable type, though, from float to string, so I would advise doing any math you want done BEFORE converting it.

You're more than welcome to use the VS Round function, but it is unlikely we will ever ship it with default content since it's getting outdated.

Link to comment
  • Marionette Maven

I also want to clarify what you're seeing with python's Round function.


Note: The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.


The documentation for the built-in round() function says that it rounds to the nearest value, rounding ties away from zero. Since the decimal fraction 2.675 is exactly halfway between 2.67 and 2.68, you might expect the result here to be (a binary approximation to) 2.68. It’s not, because when the decimal string 2.675 is converted to a binary floating-point number, it’s again replaced with a binary approximation, whose exact value is


Since this approximation is slightly closer to 2.67 than to 2.68, it’s rounded down.

Edited by MarissaF
Link to comment

Thanks for all of the info. I can get what I need from this.

One last question - is there a way to convert a node's output from a float to something that will round properly? After some experimenting it seems rounding is required before converting to a string. I can find a node that converts to a Float, but nothing that converts from a Float to a real number for example.

(I suppose this may all be solved when you finish updating the nodes so the follow the rounding display precision....)


Link to comment

Kevin, this one rounds the numbers

Hans-Olav set this up, Marissa added the add thousands and sub-string and I added the text formatting.

Just move the object between the design layer in the OIP and the note changes

Also the rounding node rounds up and down by the <> 0.5 of the result in the file attached.

Hope I am not missing the point again?




Edited by Marissa Farrell
Attached files
Link to comment

Thanks Alan. Your prodding made me go back at look at things again and I tracked down the cause.

It turns out the problem is my Concat node. Not sure why. Even though its getting a rounded number as input and its only supposed to combine strings, it somehow adds back the extra zeros. It may be a limitation of the VS Concat command.

I really wish we could use the Debug mode to trace things within a Marionette object.....

And that selecting the Node dropdown menu automatically switched back out of the Debug mode....



Edited by Kevin McAllister
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...