 # Round numbers to 1 decimal

Greetings!

I have a problem with rounding numbers to 1 decimal.

As you see in the picture above numbers like 0.85 round down to 0.8 when i want it to be 0.9. I did try using Python with the round and decimal function without luck.

Thanks!

Best regards
Tobias

Hello Tobias,

Python uses the round-half-to-even strategy to avoid introducing systematic bias into rounded numbers.

``````import math

def round_half_up(n, decimals=0):
multiplier = 10 ** decimals
return math.floor(n*multiplier + 0.5) / multiplier
``````

-Graham

1 Like

Outstanding, thank you Graham.

Does this mean that you have to put (x*multiplier + 0.55) for one decimal placement? Because from what i see in the article this give the same solution?

Best regards
Tobias

For one decimal place you would use :

``````import math

def round_half_up(n, decimals=0):
multiplier = 10 ** decimals
return math.floor(n*multiplier + 0.5) / multiplier

print round_half_up(0.85, decimals = 1)
``````

0.9

3 Likes

Thank you to both of you!

I still have truble, as you see the code don’t round up (0.85 -> 0.8) and script round some times up (0.81 -> 0.9). Might it be my input data? Where can I read all the decimals on my data? It is internalized i the attached file.

DecimalRoundCeil.gh (11.4 KB)

Best regards
Tobias

I think pufferfish have component to do round and ceiling at the same time
Also @Michael_Pryor posted a solution long time ago for this

1 Like

Yes round to decimal component

2 Likes

Thank you everyone for great response. I guess it is some issues with my data, which has more decimals than i can see from the component. I will try to check the raw data.

Turn off special case integers and adjust your decimal display. Turning off the special case integers will let you have 1.0 instead of 1.

2 Likes

Outstanding, this was the issue. Now it works perfectly, thank you very much.

Best regards
Tobias

1 Like