Java中从指数形式到十进制的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1826065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:00:36  来源:igfitidea点击:

Conversion from exponential form to decimal in Java

java

提问by sai

I want to convert exponential to decimal. e.g. 1.234E3to 1234.

我想将指数转换为十进制。例如1.234E31234

回答by Ben Hammond

you can turn it into a String using DecimalFormat

您可以使用DecimalFormat将其转换为字符串

回答by b.roth

It is not really a conversion, but about how you displaythe number. You can use NumberFormatto specify how the number should be displayed.

这不是真正的转换,而是关于您如何显示数字。您可以使用NumberFormat来指定数字的显示方式。

Check the difference:

检查差异:

double number = 100550000.75;
NumberFormat formatter = new DecimalFormat("#0.00");

System.out.println(number);
System.out.println(formatter.format(number));

回答by Edi

How about BigDecimal.valueOf(doubleToFormat).toPlainString()

怎么样 BigDecimal.valueOf(doubleToFormat).toPlainString()

回答by arul

the answer by @b.roth is correct only if it is country specific. I used that method and got i18n issue , because the new DecimalFormat("#0.00)takes the decimal seperator of the particular country. For ex if a country uses decimal seperation as "," , then the formatted value will be in 0,00 ( ex.. 1.2e2 will be 120.00 in some places and 120,00 ) in some places due to i18n issue as said here..

@b.roth 的答案只有在特定于国家/地区时才是正确的。我使用了该方法并得到了 i18n 问题,因为它new DecimalFormat("#0.00)采用特定国家/地区的十进制分隔符。对于前如果一个国家使用十进制分离-如“”,然后格式化值将在0,00(前.. 1.2e2会在一些地方和120.00 120,00)在一些地方,由于国际化问题,因为说这里..

the method that i prefer is `(new BigDecimal("1.2e2").toPlainString() )

我更喜欢的方法是`(new BigDecimal("1.2e2").toPlainString() )

回答by user2334899

just add following tag to jspx:-

只需将以下标签添加到 jspx:-

<f:convertNumber maxFractionDigits="4" minFractionDigits="2" groupingUsed="false"/>

回答by Ameer

String data =  Long.toString((long) 3.42E8);
System.out.println("**************"+data);

回答by SIVAKUMAR.J

try the following

尝试以下

long l;
double d;  //It holds the double value.such as 1.234E3 
l=Double.valueOf(time_d).longValue();

you get the decimal value in the variable l.

你得到变量 l 中的十进制值。

回答by Pratik Butani

While working with Doubles and Long numbers in Java you will see that most of the value are displayed in Exponential form.

在 Java 中使用 Doubles 和 Long 数字时,您会看到大部分值都以指数形式显示。

For Example: In following we are multiplying 2.35 with 10000 and the result is printed.

例如:下面我们将 2.35 乘以 10000,然后打印结果。

//Division example
Double a = 2.85d / 10000;
System.out.println("1. " + a.doubleValue());

//Multiplication example
a = 2.85d * 100000000;
System.out.println("2. " + a.doubleValue());

Result:

结果:

  1. 2.85E-4
  2. 2.85E8
  1. 2.85E-4
  2. 2.85E8

Thus you can see the result is printed in exponential format. Now you may want to display the result in pure decimal format like: 0.000285 or 285000000. You can do this simply by using class java.math.BigDecimal. In following example we are using BigDecimal.valueOf()to convert the Double value to BigDecimaland than .toPlainString()to convert it into plain decimal string.

因此您可以看到结果以指数格式打印。现在您可能希望以纯十进制格式显示结果,例如:0.000285 或 285000000。您只需使用 class 即可完成此操作java.math.BigDecimal。在下面的示例中,我们使用BigDecimal.valueOf()将 Double 值转换为BigDecimal和然后.toPlainString()将其转换为纯十进制字符串。

import java.math.BigDecimal;
//..
//..

//Division example
Double a = 2.85d / 10000;
System.out.println("1. " + BigDecimal.valueOf(a).toPlainString());

//Multiplication example
a = 2.85d * 100000000;
System.out.println("2. " + BigDecimal.valueOf(a).toPlainString());

Result:

结果:

  1. 0.000285
  2. 285000000
  1. 0.000285
  2. 285000000

The only disadvantage of the above method is that it generates long strings of number. You may want to restrict the value and round off the number to 5 or 6 decimal point. For this you can use java.text.DecimalFormatclass. In following example we are rounding off the number to 4 decimal point and printing the output.

上述方法的唯一缺点是它生成了很长的数字串。您可能希望限制该值并将数字四舍五入到 5 或 6 位小数点。为此,您可以使用java.text.DecimalFormat类。在以下示例中,我们将数字四舍五入到小数点后 4 位并打印输出。

import java.text.DecimalFormat;
//..
//..

Double a = 2.85d / 10000;
DecimalFormat formatter = new DecimalFormat("0.0000");
System.out.println(formatter .format(a));

Result:

结果:

0.0003

0.0003

I have just tried to compress this code with one line, it will print value of 'a' with two decimal places:

我刚刚尝试用一行压缩此代码,它将打印带有两个小数位的 'a' 值:

new DecimalFormat("0.00").format(BigDecimal.valueOf(a).toPlainString());

Happy Converting :)

快乐转换:)

回答by Sri

You can do:

你可以做:

BigDecimal
    .valueOf(value)
    .setScale(decimalLimit, RoundingMode.HALF_UP)
    .toPlainString()