我可以使用变量用科学记数法写一个数字吗? JAVA

时间:2021-01-03 22:34:09

Instead of writing the number : 1e4; // 10000.0 i want to do it as:

而不是写数字:1e4; // 10000.0我想这样做:

n = 4;
1en; // 10000.0

Its not possible to do that?

它不可能这样做?

1 个解决方案

#1


3  

You can use scientific notation in double literals. For example

您可以在双重文字中使用科学记数法。例如

double x = 3e4;

works just fine. This won't work if you replace 4 with a variable, so you can't write 3en. The best you can do is:

工作得很好。如果用变量替换4,这将无效,因此您无法写入3en。你能做的最好的事情是:

int a = 3;
int n = 4;

double x = Double.parseDouble(a + "e" + n);
System.out.println(x);    // 30000.0

double y = a * Math.pow(10, n);
System.out.println(y);    // 30000.0

#1


3  

You can use scientific notation in double literals. For example

您可以在双重文字中使用科学记数法。例如

double x = 3e4;

works just fine. This won't work if you replace 4 with a variable, so you can't write 3en. The best you can do is:

工作得很好。如果用变量替换4,这将无效,因此您无法写入3en。你能做的最好的事情是:

int a = 3;
int n = 4;

double x = Double.parseDouble(a + "e" + n);
System.out.println(x);    // 30000.0

double y = a * Math.pow(10, n);
System.out.println(y);    // 30000.0