Grasshopper

algorithmic modeling for Rhino

Hi

What is the magic formula to be written in an expression editor that will truncate the values to single decimal place eg: 123.456789 to 123.4?

Thanks

Views: 3060

Replies to This Discussion

the truncate component ;)

Maybe my question wasn't clear enough.

I need to round a value(s) to one digit after the integer so rounding component doesn't help.

Hi Piotr,

in fact the Round component would be enough. You could multiply by 10, use Round, then divide by 10. This would effectively give first-decimal rounding.

But in this world the "magic formula" exists as well. It looks like this:

Round(x, 1)

Here is how you can discover it yourself next time:

I hope it helps,

- Giulio
_______________
giulio@mcneel.com

Format ("{0:0.0},x) 

Format will convert the number to a string (text). Grasshopper will automatically convert text back into numbers when it has to and whenever possible, but one should typically try and avoid roundtripping to string data. It will bite you in the ass sooner or later.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

thanks for the correction and warning David:)
I replied because I understood/filled in Piotr wanted not to round the values, simply clip/trim the value (for Tag purposes). That is different from rounding a value, not?
I haven't had any ass biting definitions so far (or I just bit back, and that worked untill now:), but I'm curious about when the biting happens and why that is.

The most common case of derriere dental marks is rounding ironically enough. Floating point numbers tend to accumulate 'noise' as they pass through mathematical operations. Usually the least significant digits become corrupted, but sometimes the effects can be far-reaching. Basically, what this means is that when you expect to end up with the number 6.0 at the end of a calculation, what you actually have is 5.999999999999972 or maybe 6.00000000000051. These numbers are difficult to read and they are so very close to the 'perfect' value of 6.0 that it doesn't pay to display them in their full glory.

Grasshopper typically rounds displayed numbers to 6 decimal places, so if you rely on a higher accuracy, you will lose important information when roundtripping to text.

You are right of course about Formatting and Tags. If the text is not used again as input for a numeric operation it doesn't matter a pair of Capybara whiskers how many digits you discard.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David, Pieter

It was exactly for taging purposes :-)

Thanks for explanation.

Piotr

An advantage of the format function is that you could also discard unnecessary digits (say, 3.01 formatted to 1 decimal becomes 3 because the first decimal, .0, is irrelevant in this case). And also add units.

In this case, it would look like:

Format("{0:0.#} units", x)

  • dash (#) means only if necessary,
  • zero (0) means in any case

The Format function is also in the list above. You can see here for more information.

- Giulio
_______________
giulio@mcneel.com

You're a real poet;) and thanks for your clear answer too.
It didn't cross my mind that this formatted data would ever be used in further calculations. I'm reassured.

@Piotr: glad I interpreted your question correctly, thanks for your confirmation.

Also check this out   Fix ( (X + 0.5) * 10) / 10

# 1 :   17.4 = 17

# 2 :   17.6 = 18

Hope this helps.

Thanks guys!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service