欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用int()实现四舍五入

程序员文章站 2022-04-09 09:00:19
...

代码如下:

class Debug:
    def __init__(self):
        self.x0 = 0.4
        self.x1 = 0.5
        self.x2 = 0.6

    
    def mainProgram(self):
        self.x0 = int(self.x0 + 0.5)
        self.x1 = int(self.x1 + 0.5)
        self.x2 = int(self.x2 + 0.5)


        print(self.x0)              # 0 
        print(self.x1)              # 1
        print(self.x2)              # 1
        
        
main = Debug()
main.mainProgram()

当变量值为0.4的时候结果为0,变量值为0.50.6的时候结果为1