All decimal places round to 0.5

Hi,
I want all decimal places round to 0.5 ( Result B in the picture). Any help ?
Thank you.

Can be done like this…

5 Likes

Round all Decimals to 0.5.gh (6.0 KB)

2 Likes

I like this, but it doesn’t deliver the result asked for in the opening post, where it would seem that any decimal number that is not a whole number rounds to a x.5. Wondering if this could still be done by math rather than with Cumberland’s string operations.

Created a GHPython script that can do this:

Round all Decimals to 0.5_v2.gh (10.3 KB)

2 Likes

Thank you too much.

I missed that 11.9 result should be 11.5

1 Like

Thank you for your ideas
int(x)+ if((x-int(x)) > 0.00001,0.5,0)

1 Like

I’d be uncomfortable relying on 0.00001

That’s neat. Just a note that there’s a native function for getting the “fractional and integer parts of x, which can simplify things a bit:


241120_FloatToHalfDecimal.gh (5.1 KB)

3 Likes

Here is a solution using vanilla Grasshopper, a conditional and math:


(Edited. Mistakes were made)

What this essentially does is shift the values by 0.5, so that multiples of 0.5 can be rounded to as though they are whole number bounds. Then true whole numbers are eliminated from this process (mod=0 is excepted)

Effectively, this works just as the native function allowing any decimal number to be rounded to.

1 Like

There is another solution, based on text operations (nothing fancy, just another approach):

1 Like