관리 메뉴

프론트엔드 정복하기

파이썬 소수점 자리 수 제한 출력법 3가지 본문

패스트캠퍼스 - 자료구조와 알고리즘/파이썬 기초 문법

파이썬 소수점 자리 수 제한 출력법 3가지

GROWNFRESH 2021. 7. 19. 08:27

1. round 함수 - 반올림

2. format 서식 지정

3. f-string 서식 지정


1. round 함수

round(반올림하고자하는 값, 자릿수)

a = round(1.23456)
b = round(1.23456, 0) 
c = round(1.23456, 1)

print(f"round(1.23456) : {a}")
print(f"round(1.23456, 0) : {b}") 
print(f"round(1.23456, 1) : {c}")

// round(1.23456 : 1)
// round(1.23456 : 1.0)
// round(1.23456 : 1.2)

 

 

2. format 함수

a = "format example1 : {:.2f}".format(1.23456789) 
print(a)

// format example1 : 1.23

 

3. f-string

num1 = 1.23456789

print(f'f-string example1 : {num1:.0f}')
print(f'f-string example2 : {num1:.1f}')

// f-string example1 : 1
// f-string example1 : 1.2

 

 

포맷팅 관련 함수

https://grownfresh.tistory.com/382