[์ฝ๋ํธ๋ฆฌ/JAVA] ์์์ ๋ง์ถฐ ์ถ๋ ฅํ๊ธฐ/๋ฌ์ ๋ฌด๊ฒ ๊ตฌํ๊ธฐ
์ค์๊ฐ์ ๊ฐ๋ ๋ณ์์ ๊ฒฝ์ฐ ์์์ ์๋ฆฌ๋ฅผ ๋ง์ถฐ ์ถ๋ ฅํ๋ ๊ฒฝ์ฐ๊ฐ ๋๋ค์
์ด ๊ฒฝ์ฐ์ System.out.printf๋ฅผ ์ด์ฉํด %.?f ํํ๋ก ํฌ๋งท์ ์ง์ ํด์ฃผ๋ฉด ๋จ
๋ค์ ์ฝ๋๋ ์์์ 4์งธ์ง๋ฆฌ๊น์ง ๊ฐ์ ๋ฐ์ฌ๋ฆผํ์ฌ ์ถ๋ ฅํ๋ ์ฝ๋
JAVA ์ฝ๋
public class Main {
public static void main (String args[]) {
double a = 33.567268;
System.out.printf("%.4f", a);
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
33.5673
๋ฌธ์
https://www.codetree.ai/missions/4/problems/weight-on-the-moon?&utm_source=clipboard&utm_medium=text
๋ด๊ฐ ์ด ๋ต
public class Main {
public static void main(String[] args) {
int weight = 13;
double ratio = 0.165;
double result = weight * ratio;
System.out.print(weight + " * ");
System.out.printf("%.6f", ratio);
System.out.print(" = ");
System.out.printf("%.6f", result);
};
}
ํ๊ณ
๋๋ฆ ์ดํดํ๋ค๊ณ ์๊ฐํ๋๋ฐ ๋ฌธ์ ๋ณด์๋ง์, ๊ฐ๋จํ ๋ฌธ์ ์ธ๋ฐ๋ ํค๋งธ๋ค .. ใ ใ
ํด์ค์ ๋ณด๋ 3์ค๋ก ๊ฐ๋จํ๊ฒ ํ๋๋ฐ ๋๋ ๋๋ฌด ๊ธธ๊ฒ ๋์ด์ด ๊ฒ ๊ฐ๋ค.
public class Main {
public static void main(String[] args) {
// ๋ณ์ ์ ์ธ
int a = 13;
double b = 0.165;
// ์ถ๋ ฅ
System.out.printf("%d * %.6f = %.6f", a, b, a * b);
}
}
System.out.printf์ ์ ๋ ๊ฒ ์์ผ๋ก ์ ๊ณ ๋ค์ ๋ณ์๋ฅผ ๋ฃ๋๊ฑด ์๊ฐ๋ ๋ชปํ์ด์, ํ์ด์ ์ฐ์ ์๊ฐ๋ฐ์ ์ํ๋ ๊ฒ ๊ฐ๋ค.
์ด ๋ฌธ์ ํ๊ธฐ์ ์ ๋ฐฐ์ ๋ %d << ์ด๊ฒ๋ ๊ณ์ ์๊ฐํด์ฃผ๊ธฐ ๊น๋จน๊ณ ์์์ .. ๋ณต์ต๋ง์ด ์ด๊ธธ
printf์ ๋ํ ๊ณต๋ถ๋ฅผ ์กฐ๊ธ ๋ ํด์ผํ ๋ฏ..