sai
sai

Reputation: 61

Conversion from exponential form to decimal in Java

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

Upvotes: 6

Views: 63326

Answers (9)

Sri
Sri

Reputation: 1

You can do:

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

Upvotes: 0

Edi
Edi

Reputation: 640

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

Upvotes: 13

Pratik Butani
Pratik Butani

Reputation: 62391

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

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

//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

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 BigDecimal and than .toPlainString() to convert it into plain decimal string.

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

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.DecimalFormat class. In following example we are rounding off the number to 4 decimal point and printing the output.

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 have just tried to compress this code with one line, it will print value of 'a' with two decimal places:

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

Happy Converting :)

Upvotes: 2

SIVAKUMAR.J
SIVAKUMAR.J

Reputation: 4354

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.

Upvotes: 0

Ameer
Ameer

Reputation: 1

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

Upvotes: 0

user2334899
user2334899

Reputation: 11

just add following tag to jspx:-

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

Upvotes: 0

arul
arul

Reputation: 300

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..

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

Upvotes: 0

b.roth
b.roth

Reputation: 9532

It is not really a conversion, but about how 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));

Upvotes: 15

Ben Hammond
Ben Hammond

Reputation: 715

you can turn it into a String using DecimalFormat

Upvotes: 0

Related Questions