Formated I/O
:: ¹ҡѹ :: Java 2 v 5.0 (Tiger)
หน้า 1 จาก 1
Formated I/O
Formatter()
Formatter(Appendable buf)
Formatter(Appendable buf,Locale loc)
Formatter(Sring filename)
Throws FileNotFoundException, UnsuportedEncodingExcepion
Formatter(File outF)
Throws FileNotFoundException
Formatter(OutputStream outStrm)
Formatter(Appendable buf)
Formatter(Appendable buf,Locale loc)
Formatter(Sring filename)
Throws FileNotFoundException, UnsuportedEncodingExcepion
Formatter(File outF)
Throws FileNotFoundException
Formatter(OutputStream outStrm)
Re: Formated I/O
package printf;
import java.util.Formatter;
import static java.lang.System.out;
public class exam1 {
public static void main(String [] args) {
Formatter fmt = new Formatter();
out.println(fmt.format("Formatting %s is easy %d %f", "ʴ", 10, 98.6));
}
}
šѹ
Formatting ʴ is easy 10 98.600000
import java.util.Formatter;
import static java.lang.System.out;
public class exam1 {
public static void main(String [] args) {
Formatter fmt = new Formatter();
out.println(fmt.format("Formatting %s is easy %d %f", "ʴ", 10, 98.6));
}
}
šѹ
Formatting ʴ is easy 10 98.600000
แก้ไขล่าสุดโดย Admin เมื่อ Thu May 22, 2008 11:16 am, ทั้งหมด 1 ครั้ง
Re: Formated I/O
Formatter fmt = new Formatter();
out.println(fmt.format("Formatting %s is easy %d %f", "ʴ", 98.6, 10));
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at printf.exam1.main(exam1.java:11)
out.println(fmt.format("Formatting %s is easy %d %f", "ʴ", 98.6, 10));
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at printf.exam1.main(exam1.java:11)
Re: Formated I/O
Formatter fmt = new Formatter();
fmt.format("Formatting %S is easy %d %d", "ʴ", 10, (int)98.6);
System.out.println(fmt);
fmt.format("Formatting %S is easy %d %d", "ʴ", 10, (int)98.6);
System.out.println(fmt);
Re: Formated I/O
float xx =123456.789987f;
fmt.format("OLD %f \nNEW %.2f ", xx,xx);
out.println(fmt);
OLD 123456.789063
NEW 123456.79 //ѹлѴ
<hr size="2" width="100%">
float xx =1234.56712f;
out.println("Original : "+xx);
fmt.format("OLD %f \nNEW[.2] %.2f \nNEW[.3] %.3f ", xx,xx,xx);
out.println(fmt);
------------------------------
Original : 1234.5671
OLD 1234.567139
NEW[.2] 1234.57 // Ѵеǵ令 7
NEW[.3] 1234.567 // Ѵŧеǵ令 1
5
Original : 1234.5651 // 1 ѧ 5 Ѵ
NEW[.2] 1234.57
Original : 1234.565 // Ţͷ 5 Ѵŧ
NEW[.2] 1234.56
fmt.format("OLD %f \nNEW %.2f ", xx,xx);
out.println(fmt);
OLD 123456.789063
NEW 123456.79 //ѹлѴ
<hr size="2" width="100%">
float xx =1234.56712f;
out.println("Original : "+xx);
fmt.format("OLD %f \nNEW[.2] %.2f \nNEW[.3] %.3f ", xx,xx,xx);
out.println(fmt);
------------------------------
Original : 1234.5671
OLD 1234.567139
NEW[.2] 1234.57 // Ѵеǵ令 7
NEW[.3] 1234.567 // Ѵŧеǵ令 1
5
Original : 1234.5651 // 1 ѧ 5 Ѵ
NEW[.2] 1234.57
Original : 1234.565 // Ţͷ 5 Ѵŧ
NEW[.2] 1234.56
Re: Formated I/O
fmt.format("NEW[.2] |%10.2f| ",xx);
out.println(fmt);
Original : 1234.5651
NEW[.2] | 1234.57|
out.println(fmt);
Original : 1234.5651
NEW[.2] | 1234.57|
:: ¹ҡѹ :: Java 2 v 5.0 (Tiger)
หน้า 1 จาก 1
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ