development

파이썬 : 소수점 1 자리 만 얻기

big-blog 2020. 10. 16. 07:36
반응형

파이썬 : 소수점 1 자리 만 얻기


이 질문에 이미 답변이 있습니다.

어떻게 변환합니까 45.34531합니다 45.3?


한 자리로만 표현하려고하십니까?

print("{:.1f}".format(number)) # Python3
print "%.1f" % number          # Python2

또는 실제로 다른 소수 자리를 반올림합니까?

round(number,1)

또는 엄격하게 반올림합니까?

math.floor(number*10)/10

>>> "{0:0.1f}".format(45.34531)
'45.3'

또는 내장 라운드를 사용하십시오.

>>> round(45.34531, 1)
45.299999999999997

round(number, 1)

참고 URL : https://stackoverflow.com/questions/3400965/python-getting-only-1-decimal-place

반응형