Programming/Python

[Python] 반올림, 올림, 내림, 버림: round, ceil, floor, trunc

당닝 2021. 7. 7. 23:56
728x90
num = 123.4567

'''반올림'''
print(round(num))  # 123
print(round(num, 1))  # 123.5
print(round(num, 2))  # 123.46
print(round(num, -1))  # 120.0
print(round(num, -2))  # 100.0

import math #올림, 내림, 버림

'''올림'''
print(math.ceil(num))  # 124

'''내림'''
print(math.floor(num))  # 123

'''버림'''
print(math.trunc(num))  # 123
728x90