파이썬 python 반올림, 올림 ,내림 round ceil 간단 구현

반응형
  • 반올림 (round)
import math

number = 3.14159

rounded_number = round(number)
print(rounded_number) # 출력: 3

 

  • 올림 (ceil)
import math

number = 3.14159

rounded_up = math.ceil(number)
print(rounded_up) # 출력: 4

 

  • 내림 (floor)
import math

number = 3.14159

rounded_down = math.floor(number)
print(rounded_down) # 출력: 3

 

  • 버림
import math

number = 3.14159

rounded_towards_zero = math.copysign(math.floor(abs(number)), number)
print(rounded_towards_zero) # 출력: 3

 

반응형