[Python] round(), math.ceil(), math.floor()

sujin·2025년 6월 13일
0

꾸준 코딩테스트

목록 보기
9/24

round(number, ndigits = None)

  • round()는 파이썬의 내장함수이다.
  • number라는 숫자를 반올림 해준다.
  • ndigits 이하 자릿수에서 반올림 할 수 있다.
round(3.141592, 2)
#3.14
round(3.141592, 3)
#3.142
rount(3.141592)
#3
  • ndigits를 써주지 않으면 반올림한 정수를 반환한다.

math.ceil()

  • 무조건 올림을 해서 큰 것들 중 가장 가까운 정수를 반환한다.
import math

num = 2.4
ceiled_num = math.ceil(num)
print(ceiled_num) # 3

math.floor()

  • 무조건 내림을 해서 가장 가까운 작은 정수를 반환한다.
import math

num = 3.7
floored_num = math.floor(num)
print(floored_num) # 3

참고
round 함수 - python 공식 문서
참고문제 - 백준 2108 통계학

0개의 댓글