Menu
Question Index
...

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));
    }
}


10.0
10.504000
343,534,534,348.5687654
343,534,535,000.0
10
10.5
343,534,534,348.5
343,534,535,000.0
10.0
10.504
343,534,534,348.5
3.43534535E11
10.0
10.504
343,534,535,000.0
3.43534535E11
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Other Advanced Topics In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App