๐ก ์์์ ์๋ฆฌ ์ ํ๋ฐฉ๋ฒ (3๊ฐ์ง)
- โround ๋ฉ์๋
- format ์์ ์ง์
- f-string ํฌ๋งคํ
โ
๋ฐฉ๋ฒ 1. round ๋ฉ์๋
๐ round (๋ฐ์ฌ๋ฆผํ๊ณ ์ ํ๋ ๊ฐ, ์๋ฆฟ์)
a = round(1.23456)
b = round(1.23456, 0)
c = round(1.23456, 1)
d = round(1.23456, 2)
e = round(1.23456, 3)
f = round(1.23456, 4)
print(f"round(1.23456) : {a}") # ๊ฒฐ๊ณผ : 1
print(f"round(1.23456, 0) : {b}") # ๊ฒฐ๊ณผ : 1.0
print(f"round(1.23456, 1) : {c}") # ๊ฒฐ๊ณผ : 1.2
print(f"round(1.23456, 2) : {d}") # ๊ฒฐ๊ณผ : 1.23
print(f"round(1.23456, 3) : {e}") # ๊ฒฐ๊ณผ : 1.235
print(f"round(1.23456, 4) : {f}") # ๊ฒฐ๊ณผ : 1.2346
๋ฐฉ๋ฒ 2. format ์์ ์ง์
๐ "{ : .์ ์f}".format(์ค์)
a = "format example1 : {:.2f}".format(1.23456789)
b = "format example2 : {:.2f} / {:.3f}".format(1.23456789, 3.456789)
c = "format example3 : {0:.2f} / {1:.1f}".format(3.22521321, 10.123456)
d = "format example4 : {1:.2f} / {0:.1f}".format(3.22521321, 10.123456)
print(a) # ๊ฒฐ๊ณผ : 1.23
print(b) # ๊ฒฐ๊ณผ : 1.23 / 3.457
print(c) # ๊ฒฐ๊ณผ : 3.23 / 10.1
print(d) # ๊ฒฐ๊ณผ : 10.12 / 3.2
๋ฐฉ๋ฒ 3. f-string ํฌ๋งคํ
๐ f "{๋ณ์ : .์ ์f}"
num1 = 1.23456789
num2 = 9.87654321
print(f'f-string example1 : {num1:.0f}') #๊ฒฐ๊ณผ : 1
print(f'f-string example2 : {num1:.1f}') #๊ฒฐ๊ณผ : 1.2
print(f'f-string example3 : {num1:.2f}') #๊ฒฐ๊ณผ : 1.23
print(f'f-string example4 : {num1:.3f}') #๊ฒฐ๊ณผ : 1.235
print(f'f-string example5 : {num1:.4f}') #๊ฒฐ๊ณผ : 1.2346
print(f'f-string example6 : {num2:.0f}') #๊ฒฐ๊ณผ : 10
print(f'f-string example7 : {num2:.1f}') #๊ฒฐ๊ณผ : 9.9
print(f'f-string example8 : {num2:.2f}') #๊ฒฐ๊ณผ : 9.88
print(f'f-string example9 : {num2:.3f}') #๊ฒฐ๊ณผ : 9.877
print(f'f-string example10 : {num2:.4f}') #๊ฒฐ๊ณผ : 9.8765
728x90
'Language > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [๋ฌธ๋ฒ] ์ฌ๊ท ํจ์์ ์ ํ (0) | 2022.03.02 |
|---|---|
| [๋ฌธ๋ฒ] ๋ฆฌ์คํธ ์์ ๋ฐ๋๋ก ๋์ดํ๊ธฐ (0) | 2022.03.01 |
| [๋ฌธ๋ฒ] Set, List, Dictionary (์์ฑ ไธญ) (0) | 2022.02.21 |
| [๋ฌธ๋ฒ] List์ ์ต๋น๊ฐ ์ถ๋ ฅํ๊ธฐ (0) | 2022.02.21 |
| [๋ฌธ๋ฒ] ์ ๋๋ ์ดํฐ(Generator) (0) | 2022.02.10 |