파이썬

format

재히hj 2024. 2. 21. 21:40
728x90

문자와 숫자를 출력하고 싶을 때 형식

a=12
print(f"hello {a}")

 


 

소수점 2자리까지 표기하기

final_amount=round(bill_per_person,2)
print(f"Each person shoul pay: ${final_amount}")

-> 그러나 반올림하는것이기 때문에 항상 소수점 2자리를 표시하지는 않음

 

해결책

final_amount="{:.2f}".format(bill_per_person)
print(f"Each person should pay: ${final_amount}")
728x90