Convert scientific notation to decimal notation

后端 未结 3 1847
情歌与酒
情歌与酒 2021-01-21 02:10

There is a similar question on SO which suggests using NumberFormat which is what I have done.

I am using the parse() method of NumberFormat.

public sta         


        
相关标签:
3条回答
  • 2021-01-21 02:50

    If you take your angle as a double, rather than a String, you could use printf magic.

    System.out.printf("%.2f", 1.930000000000E+02);

    displays the float to 2 decimal places. 193.00 .

    If you instead used "%.2e" as the format specifier, you would get "1.93e+02"

    (not sure exactly what output you want, but it might be helpful.)

    0 讨论(0)
  • 2021-01-21 02:55

    Memorize the String.format syntax so you can convert your doubles and BigDecimals to strings of whatever precision without e notation:

    This java code:

    double dennis = 0.00000008880000d;
    System.out.println(dennis);
    System.out.println(String.format("%.7f", dennis));
    System.out.println(String.format("%.9f", new BigDecimal(dennis)));
    System.out.println(String.format("%.19f", new BigDecimal(dennis)));
    

    Prints:

    8.88E-8
    0.0000001
    0.000000089
    0.0000000888000000000
    
    0 讨论(0)
  • 2021-01-21 02:57

    When you use DecimalFormat with an expression in scientific notation, you need to specify a pattern. Try something like

    DecimalFormat dform = new DecimalFormat("0.###E0");
    

    See the javadocs for DecimalFormat -- there's a section marked "Scientific Notation".

    0 讨论(0)
提交回复
热议问题