๐ Python/๊ธฐ์ด
[Python] ์์์ ์ฒ๋ฆฌ 4๊ฐ์ง ๋ฐฉ๋ฒ
ใ
ใ
ใ
ใ
2022. 10. 20. 16:03
728x90
ํ์ด์ฌ์์ ์์์ ์ดํ์ ์ซ์๋ฅผ ๋ฒ๋ฆฌ๊ณ ์ ์ ๋ถ๋ถ๋ง ๋จ๊ธธ ๋ ์ฌ์ฉํ ์ ์๋ ๋ฐฉ๋ฒ
1. round() ํจ์๋ก ์์์ ์๋ ์ซ์ ๋ฐ์ฌ๋ฆผ
print(round(3.14))
print(round(3.9))
# 3
# 4
2. ceil() ํจ์๋ก ์์์ ์๋ ์ซ์ ์ฌ๋ฆผ
print(math.ceil(3.14))
print(math.ceil(3.9))
# 4
# 4
3. floor() ํจ์๋ก ์์์ ์๋ ์ซ์ ๋ด๋ฆผ
print(math.floor(3.14))
print(math.floor(3.9))
# 3
# 3
4. trunc()ํจ์๋ก ์์์ ์๋ ์ซ์ ๋ฒ๋ฆผ
print(math.trunc(3.14))
print(math.trunc(3.9))
# 3
# 3
# trunc() ํจ์์ floor()ํจ์์ ์ฐจ์ด์
floor()๋ ์์์ ์๋ ์ซ์๊ฐ ์์ ๋, ๋ ๋ฎ์ ์ ์๋ก ๋ด๋ฆผ
๋ฐ๋ฉด์ trunc()์ ๊ฒฝ์ฐ ์์์ ์๋ ์ซ์๋ง ๋ฒ๋ฆผ
print(math.floor(-3.14))
# -4
print(math.trunc(-3.14))
# -3
728x90