본문 바로가기
『 Python 』/Python

[Python] 파이썬 자리수 (올림,반올림)

by Play IT 2020. 3. 22.
반응형

반올림 round(실수,n)


소수점을 n번째 까지만 표현하고 반올림을 하고싶을때, round 함수를 사용하면된다.

 

>>> n =10/3

>>> n

0.3333333333333

>>> round(n,2)

0.33

>>> round(n,4)

0.3333

 

두번째매개변수를 비우면 소수점 첫번째자리를 반올림하여 나타낸다.

>>> round(n)

0

>>> type(round(n))

<class 'int'>

>>> type(round(n,2))

<class 'float'>

 

 

자리수에 음수를 입력하여 정수자리에 해당하는 곳에서 반올림이 가능하다.

>>> round(12345,-1)

12340

>>> round(12345,-2)

12300

 

 

 

소수점 올림,내림,버림 

math.ceil(i) : 올림, math.floor(i) : 내림, tmath.runc(i) : 버림


올림,내림,버림을 하기위해서 math 클래스안의 ceil, floor, trunc 함수를 사용해야한다.

 

>>> import math

math.ceil(i) : 올림

>>> math.ceil(9.2)

10

>>> math.ceil(9.6)

10

 

math.floor(i) : 내림

>>> math.floor(9.2)

9

>>> math.floor(9.6)

9

 

tmath.runc(i) : 버림

>>> math.trunc(9.2)

9

>>> math.trunc(9.6)

9

 

 
반응형

댓글