Menu
Topics Index
...
`

Format Doubles


If you need explanation Read this topic

If you need Answer Take test on this topic

Format Doubles
What will be the output of the following program?
import java.text.DecimalFormat;
public class FormatDoubles {
    public static void main(String[] args) {
        print(12.45);
        print(12312.45);
        print(2000.450223);
        print(3.4545454454);
    }
    public static void print(double value) {
        DecimalFormat f1 = new DecimalFormat("0000.00");
        DecimalFormat f2 = new DecimalFormat("0.0000");
        DecimalFormat f3 = new DecimalFormat("##00.00##");
        DecimalFormat f4 = new DecimalFormat("#####.#############");
        DecimalFormat f5 = new DecimalFormat("#.00");
        System.out.print(f1.format(value) + " ");
        System.out.print(f2.format(value) + " ");
        System.out.print(f3.format(value) + " ");
        System.out.print(f4.format(value) + " ");
        System.out.println(f5.format(value));
    }
}

A. 12.45 12.4500 12.45 0012.45 12.45
12312.45 12312.4500 12312.45 12312.45 12312.45
2000.450223 2000.4502 2000.4502 2000.45 2000.45
3.4545454454 3.4545 03.4545 0003.45 3.45
B. 0012.45 12.45 12.4500 0012.45 12.45
12312.45 12312.45 12312.4500 12312.45 12312.45
2000.45 2000.4502 2000.4502 2000.45 2000.45
0003.45 03.4545 3.4545 0003.45 3.45
C. 0012.45 12.4500 12.45 12.45 12.45
12312.45 12312.4500 12312.45 12312.45 12312.45
2000.45 2000.4502 2000.4502 2000.450223 2000.45
0003.45 3.4545 03.4545 3.4545454454 3.45
D. None of the above
Topic: Advanced Java Programming Concepts

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C: Decimala Format formats the given input with various patterns,,0 indicates Minimum Fraction where as # is Optional...f1 formats like 4 digits before fraction and 2 digits after fraction...so it gives output as 0012.45 like remining

Posted by Nagendra Reddy    2014-02-13 09:06:36


Congratulations Nagendra Reddy. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-14 09:21:07


Thank You MC

Posted by Nagendra Reddy    2014-02-14 11:01:35


© meritcampus 2019

All Rights Reserved.

Open In App