打印大号而不显示符号

时间:2014-08-19 09:13:02

标签: java

我想在java中打印一个没有e的大号(115411606189999999999999999999999999999994848448989)。有没有什么方法..首先我从用户那里取一个大号(可能是45位)并在控制台上显示..

package arraycalc;

/**
 *
 * @author gaurav
 */

import java.util.Scanner;

class ArrayCalc {

    /**
     * @param args
     */
    public static void main(String[] args) {

        double input1, input2;
        char operator;
        Operation o = new Operation();
        Scanner input = new Scanner(System.in);
        System.out.println("please Enter First Number");
        input1 = input.nextDouble();
        System.out.println("please Enter Second Number");
        input2 = input.nextDouble();
        System.out.println("press + for addition");
        System.out.println("press - for subtraction");
        System.out.println("press * for multiplication");
        System.out.println("press / for divide");
        operator = input.next().charAt(0);
        if ((input1 == Math.round(input1)) && (input2 == Math.round(input2))) {
            long a = (long) input1;
            long b = (long) input2;
            switch (operator) {

                case '+':
                    o.add(a, b);

                    System.exit(0);
                case '-':
                    o.sub(a, b);
                    System.exit(0);

                case '*':
                    o.multi(a, b);
                    System.exit(0);

                case '/':
                    o.div(a, b);
                    System.exit(0);
                default:
                    o.error();
                    System.exit(0);

            }
        } else {
            double a = input1;
            double b = input2;
            switch (operator) {

                case '+':
                    o.add(a, b);

                    System.exit(0);
                case '-':
                    o.sub(a, b);
                    System.exit(0);

                case '*':
                    o.multi(a, b);
                    System.exit(0);

                case '/':
                    o.div(a, b);
                    System.exit(0);
                default:
                    o.error();
                    System.exit(0);

            }
        }

    }
}

//interface declared in our class

interface addtion {
    void add(double a, double b);

    void add(long a, long b);

}

interface subtraction {
    void sub(double a, double b);

    void sub(long a, long b);
}

interface multiplication {
    void multi(double a, double b);

    void multi(long a, long b);
}

interface division {
    void div(double a, double b);

    void div(long a, long b);
}


class Operation implements addtion, subtraction, multiplication, division {
    @Override
    public void add(double a, double b) {
        try

        {
            double c = 0;
            c = a + b;
            result(c);
            //System.out.println("Addition of given numbers is " + c);

        } catch (ArithmeticException e) {
            System.out.println(e);
        }
    }

    @Override
    public void add(long a, long b) {
        try {
            long c = 0;
            c = a + b;
            result(c);
            //System.out.println("Addition of given numbers is " + c);
        } catch (ArithmeticException e) {
            System.out.println(e);
        }
    }

    @Override
    public void sub(double a, double b) {
        double c = 0;
        c = a - b;
        //System.out.println("Subtraction of given numbers is " + c);
        result(c);
    }

    @Override
    public void sub(long a, long b) {
        long c = 0;
        c = a - b;
//  System.out.println("Subtraction of given numbers is " + c);
        result(c);
    }

    @Override
    public void multi(long a, long b) {
        long c = 0;
        c = a * b;
//  System.out.println("Multiplication of given numbers is " + c);
        result(c);

    }


    @Override
    public void multi(double a, double b) {
        double c = 0;
        c = a * b;
        //System.out.println("Multiplication of given numbers is " + c);
        result(c);
    }

    @Override
    public void div(double a, double b) {
        double c = 0;
        c = a / b;
        result(c);
    }

    @Override
    public void div(long a, long b) {
        double c = 0;
        double divident = (double) a;
        double divisor = (double) b;
        c = divident / divisor;
        if (c == Math.round(c)) {
            result((long) c);
        } else {
            result(c);
        }
    }

    void error() {
        System.out.println("invalid operator");
    }

    void result(double d) {
        String e = String.valueOf(d);
        //boolean g= e.matches(".*[a-zA-Z]+.*");       
        //System.out.println(g);
        if (e.matches(".*[eE]+.*") == true) {
            System.out.println("Result is in Expoential Power " + d);

        } else {
            System.out.println("Result is " + d);
        }
    }

    void result(long d) {
        String e = String.valueOf(d);
        //boolean g= e.matches(".*[a-zA-Z]+.*");       
        //System.out.println(g);
        if (e.matches(".*[eE]+.*") == true) {
            System.out.println("Result is in Expoential Power " + d);

        } else {
            System.out.println("Result is " + d);
        }
    }

}

1 个答案:

答案 0 :(得分:2)

不是那么难。您可以轻松使用BigDecimal

String str = "11541160618999999999999999999999999999999948447989";
double d = Double.parseDouble(str);
System.out.println(d);
BigDecimal bg = new BigDecimal(str);
System.out.println(bg); 

输出:

1.1541160619E49
11541160618999999999999999999999999999999948447989