Menu
Topics Index
...
`

Format object


If you need explanation Read this topic

If you need Answer Take test on this topic

Format object
What will be the output of the following program?
import java.math.*;
import java.text.*;
import java.util.*;
public class NumberUtility {
    public static String change(Object num) {
        BigDecimal num1 = new BigDecimal(num.toString());
        num1 = num1.stripTrailingZeros();
        String str = "###,##0.0###########";
        DecimalFormat newf = new DecimalFormat(str, new DecimalFormatSymbols(Locale.US));
        return newf.format(num1);
    }
    public static double change1(Object number) {
        try {
            StringBuffer inputNo = new StringBuffer(number.toString());
            if (inputNo.length() > 0) {
                while (inputNo.indexOf(",") != -1) {
                    inputNo.deleteCharAt(inputNo.indexOf(","));
                }
            } else {    return 0.0;    }
            return Double.parseDouble(inputNo.toString());
        } catch (NumberFormatException e) {    return 0.0;    }
    }
    public static void main(String args[]) {
        int i = 10;
        double d = 10.504000;
        float f = 343534534348.5687654F;
        String str = "343,534,535,000.0";
        System.out.println(NumberUtility.change(new Integer(i)));
        System.out.println(NumberUtility.change(new Double(d)));
        System.out.println(NumberUtility.change(new Float(f)));
        System.out.println(NumberUtility.change1(str));
    }
}

A. 10.0
10.504000
343,534,534,348.5687654
343,534,535,000.0
B. 10
10.5
343,534,534,348.5
343,534,535,000.0
C. 10.0
10.504
343,534,534,348.5
3.43534535E11
D. 10.0
10.504
343,534,535,000.0
3.43534535E11
E. Some other output
F. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

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.

F.compilation error shall happnen as float cannot handle. it.

Posted by Shabaaz Shaikz    2014-02-20 13:09:19


Ans is D

Posted by Kittu Manoj    2014-02-20 13:16:09


i,d,f are represented in the mentioned format of "str" and str (belonging to main) is expressed as Double value throuch chage1(......)

Posted by Kittu Manoj    2014-02-20 13:21:47


Answer is F.... becoz  float cannot handle such a large value....thus it is compilation error..

Posted by Sneha Rapool    2014-02-20 14:18:36


Answer is D. BigDecimal object is constructed for i, d and f in the change method and then any trailing zeros are removed. Then DecimalFormat is constructed with given pattern and symbol. The format method rounds off the result (num1 here) when it is required. change1 method first removes all commas from str and parseDouble method takes this str and returns a corresponding double value (in E notation here).

Posted by Shaileshwar Sharma    2014-02-20 16:31:41


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

Posted by Merit Campus    2014-02-21 05:36:14


© meritcampus 2019

All Rights Reserved.

Open In App