Phương thức này nhận format String và arguments (varargs) trả về String object của biến theo format chỉ định.
Có thể format double thành String dùng format(). Truyền "%f" làm format string (kèm giá trị double).
Ví dụ 1
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);
}
}
Đầu ra
Enter a double value: 2548.2325 The result is: 2548.2325
Ví dụ 2
public class Sample {
public static void main(String args[]) {
double val = 22588.336;
String str = String.format("%f", val);
System.out.println(str);
}
}
Đầu ra
22588.336000