올림
math.ceil()
>>> import math
>>> math.ceil(-3.14)
-3
>>> math.ceil(3.14)
4
내림
math.floor()
>>> import math
>>> math.floor(-3.14)
-4
>>> math.floor(3.14)
3
버림
math.trunc()
>>> import math
>>> math.floor(-3.14)
-4
>>> math.trunc(-3.14)
-3
>> int(-3.14)
-3
반올림
round()
round(숫자,자릿수)으로 사용
- 두번째 인자 생략시 소수 첫째자리에서 반올림
- 두번째 인자에 음수 사용 가능
- ex.
- 1의 자리에서 반올림 : round(num, -1)
- 10의 자리에서 반올림 : round(num, -2)
- 소수 첫째 자리에서 반올림 : round(num)
- 소수 둘째 자리에서 반올림 : round(num, 1)
- 소수 셋째 자리에서 반올림 : round(num, 2)
>>> round(3.1415)
3
>>> round(3.1415, 2)
3.14
>>> round(31.415, -1)
30.0
사사오입 원칙
- 반올림할 자리의 수가 5인 경우
- 앞자리 숫자가 짝수면 내림
- 앞자리 숫자가 홀수면 올림
>>> round(4.5)
4
>>> round(3.5)
4