简体   繁体   中英

Round up float field

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))

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM