YUYANE

Python / float 소수점 자리 표기, 숫자에 콤마(,) 넣기 본문

Programming Languages/PYTHON

Python / float 소수점 자리 표기, 숫자에 콤마(,) 넣기

YUYA 2021. 1. 30. 16:19

목표

 

- float 형의 숫자를 소수점 아래 2자리 까지만 표기하자.

- 1000 단위로 숫자에 콤마/반점/',' 표시를 넣어주자.

 

 

소수점 자리 표기 정하기 (소수점 2자리까지 표기하는 코드)

 

value = 3.333333

# 소수점 2자리까지 표기
formatted_string = "{:.2f}".format(value)

#3.33
print(formatted_string)

#출처 : https://www.kite.com/python/answers/how-to-specify-the-number-of-decimal-places-in-python

 

 

숫자에 반점/콤마 표시 하기

 

number_with_commas = "{:,}".format(1000000)

#1,000,000
print(number_with_commas)

#출처 : https://www.kite.com/python/answers/how-to-add-commas-to-a-number-in-python

 

 

소수점 자리와 콤마 표기를 동시에 하고 싶다면?

 

amounts_format = "{:,.2f}".format(1000000.33333)


#1,000,000.33
print(amounts_format)

 

Comments