简体   繁体   English

将BigDecimal转换为double值

[英]Converting BigDecimal to double value

I want to ask how to transform all my String to double with exponential. 我想问一下如何将我所有的String转换为指数倍。 when I use the string that length is over seven it's doing fine . 当我使用长度超过7的字符串时,效果很好。

new BigDecimal("12345678").doubleValue() => 1.2345678E7

but seven and under I can't export exponential number. 但是在7岁以下,我无法导出指数数。

new BigDecimal("1234567").doubleValue() => 1234567.0

what I want is like 1.234567E6. 我想要的像是1.234567E6。

Is there any way to do this? 有什么办法吗? I've been searching for a while ,but got nothing. 我已经搜索了一段时间,但一无所获。

The problem is the type I return must be double . 问题是我返回的类型必须为double。 After transforming the value under seven I can only get the value without exponential. 在将值转换为7以下后,我只能获得没有指数的值。

double test = new BigDecimal("1.234567E6").doubleValue() ;//output 1234567.0 

but I need it to be 1.234567E6 and return to caller. 但我需要将其设为1.234567E6并返回给呼叫者。 Is that Impossible? 那不可能吗?

For your problem, you want to convert the value to the BigDecimal with exponential, you can use the DecimalFormat . 对于您的问题,您想将值转换为带指数的BigDecimal ,可以使用DecimalFormat You can also change the scale for the output value digits. 您也可以更改输出值数字的比例。

import java.math.*;
import java.text.*;
public class HelloWorld{

     public static void main(String []args){
        double a = new BigDecimal("1234567").doubleValue();
        String b;
        System.out.println(a);

        NumberFormat formatter = new DecimalFormat("0.0E0");
        formatter.setRoundingMode(RoundingMode.DOWN);
        formatter.setMinimumFractionDigits(5); //<---Scale
        b = formatter.format(a);

        System.out.println(b);
     }
}

The output will be like: 输出将类似于:

1234567.0 //Unformatted Value
1.23456E6 //Formatted Value

You should know that 1.2345678e7 and 12345678.0 are exactly the same value, only with different textual representations . 您应该知道1.2345678e712345678.0完全相同的值,只是具有不同的文本表示形式 You could represent 1234567.0 as 1.234567e6 too. 你可以代表1234567.0作为1.234567e6了。 Also exactly the same double, just a different way of writing it out. 同样是完全相同的double,只是写出来的方式不同。

The default output shows values with more than a certain number of significant digits in exponential format ("e-form"), otherwise as plain decimal format. 默认输出以指数格式(“ e-form”)显示具有多个有效位数的值,否则以纯十进制格式显示。

So you may want to change the formatting of the doubles you receive. 因此,您可能需要更改收到的双打的格式。 This can be done with eg DecimalFormat or String.format() or similar. 这可以通过例如DecimalFormatString.format()或类似方法来完成。 That does not change the doubles, only the way they are presented in a string. 这不会改变双打,只会改变它们在字符串中的显示方式。

See the section about Scientific Notation in java.text.DecimalFormat . 请参阅java.text.DecimalFormat中有关科学计数法的部分。

For example, 例如,

    DecimalFormat scientificFormat = new DecimalFormat("0.###E0");
    System.out.println(scientificFormat.format(BigDecimal.valueOf(123456L)));
    System.out.println(scientificFormat.format(BigDecimal.valueOf(1234567L)));

    scientificFormat.setMinimumFractionDigits(10);
    System.out.println(scientificFormat.format(BigDecimal.valueOf(12345678L)));

would give you 会给你

1,235E5
1,235E6
1,2345678000E7

Change the pattern to match what you're looking for. 更改模式以匹配您要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM