0

I am am trying to get a float field to round up to next number in Odoo 10. I am calcuating the room width by room lenght

@api.onchange('roomwidth')
    def _onchange_roll_required(self):
        self.rolls_required = (self.roomwidth or 1) / (self.carpet_width or 1)

The carpet width is 6 so a 13x9 room works out at 2.17. I need this 2.17 to be 3 so I need to round up to next number. I have used the below for 3 digits but never rounded up

rolls_required = fields.Float("Rolls Required",digits=(12,3))
CZoellner
  • 13,553
  • 3
  • 25
  • 38
Rocky Jones
  • 85
  • 1
  • 11
  • 3
    [**`math.ceil`**](https://docs.python.org/3/library/math.html#math.ceil) – Peter Wood Sep 20 '18 at 11:37
  • 1
    Possible duplicate of [How do you round UP a number in Python?](https://stackoverflow.com/questions/2356501/how-do-you-round-up-a-number-in-python) – mkrieger1 Sep 20 '18 at 12:26

2 Answers2

0

use the built in round() function.

ex :

a = 3.93

print(round(a, 0)) // 4.0

The round function takes 2 args, the second being which number to round up

EDIT :

oh ! Sorry about that ! here, try this :

a = 2.17
def rnd(a):
    if((a+1)-a >= 0.5):
        return int(a+1)
    else:
        return round(a, 0)
print(rnd(a)) // 3
lalam
  • 195
  • 1
  • 11
  • but this will not work as required in question - from 2.17 it will give 2, but expected 3 – Drako Sep 20 '18 at 12:12
  • he has to write custom round function which checks if there is something after decimal - it rounds base to +1 – Drako Sep 20 '18 at 12:13
-2

As Drako mentioned in his comment, you need to check there is something after decimal then add 1 to base. Try this:

num = 2.17
if num % 1 != 0:
    rounded_num = int(num+1)
print(rounded_num)
Rahul Raut
  • 1,099
  • 8
  • 15