Converting from exponential to decimal in Java

I want to convert the exponent to decimal. for example 1.234E3before 1234.

+5
source share
9 answers

This is actually not a conversion, but the way you display the number. You can use NumberFormat to specify how the number should be displayed.

Check the difference:

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

System.out.println(number);
System.out.println(formatter.format(number));
+15
source

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

+11
source
0

by @b.roth , . i18n , new DecimalFormat("#0.00) . , ",", 0,00 (, 1.2e2 120,00 ​​120,00) - i18n, .

, , `( BigDecimal ( "1.2e2" ). toPlainString())

0

jspx: -

<f:convertNumber maxFractionDigits="4" minFractionDigits="2" groupingUsed="false"/>
0
String data =  Long.toString((long) 3.42E8);
System.out.println("**************"+data);
0

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

l.

0

Java , .

: 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());

:

  • 2.85E-4
  • 2.85E8

, , . , : 0.000285 285000000. , 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());

:

  • 0.000285
  • 285000000

The only drawback of the above method is that it generates long strings of numbers. You can limit the value and round the number to 5 or 6 decimal places. You can use a class for this java.text.DecimalFormat. In the following example, we round the number to 4 decimal places and print the result.

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

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

Result:

0.0003

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

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

Happy conversion :)

0
source

You can do:

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

All Articles