เมธอดนี้ยอมรับรูปแบบสตริงและอาร์กิวเมนต์ (varargs) และส่งคืนอ็อบเจ็กต์ String ของตัวแปรที่กำหนดในรูปแบบที่ระบุ
คุณสามารถจัดรูปแบบค่าสองเท่าเป็นสตริงโดยใช้เมธอด format() ผ่าน “%f” เป็นสตริงรูปแบบ (พร้อมกับค่าคู่ที่จำเป็น)
ตัวอย่าง
import java.util.Scanner;
public class ConversionOfDouble {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a double value:");
double d = sc.nextDouble();
String result = String.format("%f", d);
System.out.println("The result is: "+result);
}
} ผลลัพธ์
Enter a double value: 2548.2325 The result is: 2548.2325
ตัวอย่าง
public class Sample{
public static void main(String args[]){
double val = 22588.336;
String str = String.format("%f", val);
System.out.println(str);
}
} ผลลัพธ์
22588.336000