Java์์ ์ซ์๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ์ ๋จ์ํ ๊ฐ ์ถ๋ ฅ์์๋ถํฐ ํ์ํ๋ ์ซ์ ํํ๊น์ง ๋ค์ํ๊ฒ ํ์ฉ๋ฉ๋๋ค. ์ซ์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์, ์ค์, ๊ทธ๋ฆฌ๊ณ ์์ํ๋ ๋ฌธ์์ด๋ก ์ถ๋ ฅํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ํตํด ์์์ ์๋ฆฌ์ ์กฐ์ , ์ฒ ๋จ์ ๊ตฌ๋ถ, ํ์ ์ง์ ๋ฑ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ด๋ฒ ๊ธ์์๋ Java์์ ์ซ์๋ฅผ ์ถ๋ ฅํ๋ ๋ฐฉ๋ฒ๊ณผ ํจ๊ป ์ซ์ ํฌ๋งท์ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ๊ฒ ์ต๋๋ค.
๊ธฐ๋ณธ ์ซ์ ์ถ๋ ฅ: System.out.print์ println
Java์์ ์ซ์๋ฅผ ์ถ๋ ฅํ๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ System.out.print
์ System.out.println
์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. ์ด ๋ฉ์๋๋ ์ซ์ ํ์
์ ๊ฐ๋ฆฌ์ง ์๊ณ ๋ชจ๋ ์ซ์๋ฅผ ๊ทธ๋๋ก ์ถ๋ ฅํด์ค๋๋ค.
์์ ์ฝ๋
public class BasicPrintExample {
public static void main(String[] args) {
int intNumber = 100;
double doubleNumber = 3.14159;
System.out.print("์ ์ ์ถ๋ ฅ: ");
System.out.println(intNumber);
System.out.print("์ค์ ์ถ๋ ฅ: ");
System.out.println(doubleNumber);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์ ์ ์ถ๋ ฅ: 100
์ค์ ์ถ๋ ฅ: 3.14159
์ ์ฝ๋์์๋ ์ ์์ ์ค์๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ถ๋ ฅํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
์ซ์ ํ์ ์ง์ : System.out.printf
Java์์๋ System.out.printf
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ซ์ ์ถ๋ ฅ์ ํ์ํํ ์ ์์ต๋๋ค. printf
๋ ํ์ ๋ฌธ์์ด์ ์ฌ์ฉํ์ฌ ์์์ ์๋ฆฌ์, ์ ๋ ฌ ๋ฐฉ์ ๋ฑ์ ์ง์ ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
์ฃผ์ ํ์ ์ง์ ์
%d
: ์ ์ ์ถ๋ ฅ%f
: ์ค์ ์ถ๋ ฅ (๊ธฐ๋ณธ์ ์ผ๋ก ์์์ ์ดํ 6์๋ฆฌ)%.nf
: ์์์ ์ดํ n์๋ฆฌ๊น์ง ์ค์ ์ถ๋ ฅ
์์ ์ฝ๋
public class PrintfExample {
public static void main(String[] args) {
int number = 1234;
double pi = 3.14159265359;
System.out.printf("์ ์ ์ถ๋ ฅ: %d%n", number);
System.out.printf("์์์ ์ดํ 2์๋ฆฌ ์ค์ ์ถ๋ ฅ: %.2f%n", pi);
System.out.printf("์์์ ์ดํ 4์๋ฆฌ ์ค์ ์ถ๋ ฅ: %.4f%n", pi);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์ ์ ์ถ๋ ฅ: 1234
์์์ ์ดํ 2์๋ฆฌ ์ค์ ์ถ๋ ฅ: 3.14
์์์ ์ดํ 4์๋ฆฌ ์ค์ ์ถ๋ ฅ: 3.1416
printf
๋ฅผ ์ฌ์ฉํ๋ฉด ์์์ ์๋ฆฌ์๋ฅผ ์กฐ์ ํ๊ฑฐ๋ ์ ์๋ฅผ ์ํ๋ ํ์์ผ๋ก ํํํ ์ ์์ต๋๋ค.
DecimalFormat์ ์ด์ฉํ ์ซ์ ํฌ๋งท ์ง์
DecimalFormat
ํด๋์ค๋ ์ซ์๋ฅผ ํ์ํ๋ ๋ฌธ์์ด๋ก ๋ณํํ๋ ๋ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ํนํ, ์ฒ ๋จ์ ๊ตฌ๋ถ ๊ธฐํธ ์ถ๊ฐ, ์์์ ์๋ฆฌ์ ์กฐ์ ๋ฑ์ ์ ๋ฆฌํฉ๋๋ค.
DecimalFormat ์ฌ์ฉ ์์
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
double salary = 1234567.89;
DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println("ํ์ํ๋ ์ซ์ ์ถ๋ ฅ: " + formatter.format(salary));
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
ํ์ํ๋ ์ซ์ ์ถ๋ ฅ: 1,234,567.89
์ ์ฝ๋์์๋ #,###.00
ํ์์ ์ฌ์ฉํ์ฌ ์ฒ ๋จ์ ๊ตฌ๋ถ ๊ธฐํธ์ ์์์ ๋์งธ ์๋ฆฌ๊น์ง์ ์ซ์ ํฌ๋งท์ ์ง์ ํ์ต๋๋ค.
String.format์ ์ด์ฉํ ์ซ์ ํฌ๋งทํ
String.format
๋ฉ์๋๋ printf
์ ์ ์ฌํ ๋ฐฉ์์ผ๋ก ํ์์ ์ง์ ํ์ฌ ๋ฌธ์์ด์ ์์ฑํฉ๋๋ค. ์ด๋ฅผ ํตํด ์์์ ์๋ฆฌ์๋ฅผ ์ ์ดํ๊ฑฐ๋ ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ ์ ์์ต๋๋ค.
์์ ์ฝ๋
public class StringFormatExample {
public static void main(String[] args) {
double num = 123.45678;
String formatted = String.format("์์์ ์ดํ 2์๋ฆฌ: %.2f", num);
System.out.println(formatted);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์์์ ์ดํ 2์๋ฆฌ: 123.46
String.format
์ printf
์ ๋น์ทํ๊ฒ ์ฌ์ฉ๋์ง๋ง, ์ถ๋ ฅ๋ ๋ฌธ์์ด์ ๋ฐํํ ์ ์์ด ๋ฌธ์์ด ์กฐ์์ด ํ์ํ ๋ ์ ์ฉํฉ๋๋ค.
NumberFormat์ ์ด์ฉํ ํตํ ๋ฐ ํผ์ผํธ ํ์
NumberFormat
ํด๋์ค๋ ํตํ์ ํผ์ผํธ ํ์์ ์ง์ ํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค. NumberFormat
์ ์ง์ญํ ์ง์์ ํตํด ํตํ ๊ธฐํธ์ ํผ์ผํธ ๊ธฐํธ๋ฅผ ์๋์ผ๋ก ์ ์ฉํฉ๋๋ค.
ํตํ ํ์ ์์
import java.text.NumberFormat;
public class CurrencyFormatExample {
public static void main(String[] args) {
double money = 1500.75;
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
System.out.println("ํตํ ํ์: " + currencyFormatter.format(money));
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
ํตํ ํ์: $1,500.75 (์ง์ญ์ ๋ฐ๋ผ ๊ธฐํธ๊ฐ ๋ฌ๋ผ์ง ์ ์์)
์ ์ฝ๋์์๋ NumberFormat.getCurrencyInstance()
๋ฅผ ์ฌ์ฉํด ์ง์ญ์ ๋ง๋ ํตํ ๊ธฐํธ์ ํจ๊ป ๊ธ์ก์ ํ์ํฉ๋๋ค.
ํผ์ผํธ ํ์ ์์
public class PercentFormatExample {
public static void main(String[] args) {
double percentage = 0.85;
NumberFormat percentFormatter = NumberFormat.getPercentInstance();
System.out.println("ํผ์ผํธ ํ์: " + percentFormatter.format(percentage));
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
ํผ์ผํธ ํ์: 85%
์ด ์ฝ๋๋ NumberFormat.getPercentInstance()
๋ฅผ ํตํด ์์์ ์ดํ ๊ฐ์ ์๋์ผ๋ก ํผ์ผํธ ํ์์ผ๋ก ๋ณํํ์ฌ ์ถ๋ ฅํฉ๋๋ค.
๋ค์ํ ์ซ์ ํฌ๋งท ์กฐํฉ ์์
์ง๊ธ๊น์ง ์ดํด๋ณธ ๋ค์ํ ์ซ์ ์ถ๋ ฅ ๋ฐฉ๋ฒ์ ์กฐํฉํ์ฌ ๋ณด๋ค ๋ณต์กํ ์ถ๋ ฅ ํ์์ ์ง์ ํ ์ ์์ต๋๋ค. ์๋ ์์ ์์๋ ํตํ, ํผ์ผํธ, ์ผ๋ฐ ์ซ์ ํ์์ ํจ๊ป ์ถ๋ ฅํด ๋ด ๋๋ค.
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class CombinedFormatExample {
public static void main(String[] args) {
double salary = 55000.5;
double growthRate = 0.073;
double sales = 1234567.89;
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
NumberFormat percentFormatter = NumberFormat.getPercentInstance();
DecimalFormat salesFormatter = new DecimalFormat("#,###.00");
System.out.println("์๊ธ: " + currencyFormatter.format(salary));
System.out.println("์ฑ์ฅ๋ฅ : " + percentFormatter.format(growthRate));
System.out.println("๋งค์ถ: " + salesFormatter.format(sales));
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์๊ธ: $55,000.50
์ฑ์ฅ๋ฅ : 7%
๋งค์ถ: 1,234,567.89
์ด ์์ ์์๋ NumberFormat
๊ณผ DecimalFormat
์ ์กฐํฉํ์ฌ ํตํ, ํผ์ผํธ, ์ฒ ๋จ์ ๊ตฌ๋ถ ํ์์ ๊ฐ๊ฐ ์ง์ ํ์ฌ ์ถ๋ ฅํ๊ณ ์์ต๋๋ค.
๊ฒฐ๋ก
Java์์ ์ซ์๋ฅผ ์ถ๋ ฅํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ์ดํด๋ณด์์ต๋๋ค. System.out.print
, System.out.printf
, DecimalFormat
, NumberFormat
๋ฑ์ ํตํด ์ ์์ ์ค์๋ฅผ ์์ฝ๊ฒ ์ถ๋ ฅํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ํตํด ์์์ ์๋ฆฌ์ ์กฐ์ , ์ฒ ๋จ์ ๊ตฌ๋ถ, ํตํ ๋ฐ ํผ์ผํธ ํ์ ๋ฑ ๋ค์ํ ํฌ๋งท์ ์ค์ ํ ์ ์์ต๋๋ค. Java์ ๋ค์ํ ์ซ์ ์ถ๋ ฅ ๋ฐฉ๋ฒ์ ํ์ฉํด ๊น๋ํ๊ณ ๊ฐ๋
์ฑ ์ข์ ์ถ๋ ฅ์ ๊ตฌํํด ๋ณด์ธ์!
๋๊ธ ์ฐ๊ธฐ