gpt4 book ai didi

java - 打印大数字而不显示 e 符号

转载 作者:行者123 更新时间:2023-12-02 05:32:34 25 4
gpt4 key购买 nike

我想在java中打印一个没有e的大数字(11541160618999999999999999999999999999999948447989)。有什么方法..首先我从用户那里获取大量数字(可能是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);
}
}

}

最佳答案

这并不难。您可以轻松使用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

关于java - 打印大数字而不显示 e 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25379654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com