-1

I have searched the internet but I could not find any solution (maybe, I've searched badly).
I want to convert the String "108595000.5" to a double and I have used these methods:

Double.parseDouble("108595000.5");
Double.valueOf("108595000.5");

Unfortunately, both of them return 1.08595E8.
How can I convert this String to double without problem?

deHaar
  • 17,687
  • 10
  • 38
  • 51
m n
  • 223
  • 1
  • 7
  • 19
  • 4
    `1.08595E8'` is a correct representation of 108595000.5 number, just written in scientific notation. – Yrii Borodkin Mar 23 '20 at 08:33
  • Whats the issue in output ? double will return that only – Sanjeev Mar 23 '20 at 08:33
  • If you are caring about output, use a `BigDecimal`... It will hold the correct `double` but can easily be printed in different notations. – deHaar Mar 23 '20 at 08:34
  • Does this answer your question? [How do I print a double value without scientific notation using Java?](https://stackoverflow.com/questions/16098046/how-do-i-print-a-double-value-without-scientific-notation-using-java) – ADM Mar 23 '20 at 08:34
  • 2
    It's hard to wrap your head around it but a `double` doesn't "hold a scientific notation" or any notation at all. It represents a number. How that number is printed depends on how you print it. – Joachim Sauer Mar 23 '20 at 08:38
  • I want to do mathematical operations on it so that I get into trouble. I want the real number – m n Mar 23 '20 at 09:12
  • 1
    You are confusing presentation (printing the value) with the actual value of the `double` variable. – Mark Rotteveel Mar 23 '20 at 09:29
  • Which *mathematical operations* are you planning to do with that number? – deHaar Mar 23 '20 at 09:31

2 Answers2

5

The methods you have used do not return 1.08595E8, instead, they return the number and what you are complaining about is the representation of that number in the console (or as a String).

However, you can specify how to output a doubleyourself with a specified formatting, see this example:

public static void main(String[] args) {
    String value = "108595000.5";
    // use a BigDecimal to parse the value
    BigDecimal bd = new BigDecimal(value);
    // choose your desired output:
    // either the String representation of a double (undesired)
    System.out.println("double:\t\t\t\t\t" + bd.doubleValue());
    // or an engineering String
    System.out.println("engineering:\t\t\t\t" + bd.toEngineeringString());
    // or a plain String (might look equal to the engineering String)
    System.out.println("plain:\t\t\t\t\t" + bd.toPlainString());
    // or you specify an amount of decimals plus a rounding mode yourself
    System.out.println("rounded with fix decimal places:\t" 
                        + bd.setScale(2, BigDecimal.ROUND_HALF_UP));
}
double:                             1.085950005E8
engineering:                        108595000.5
plain:                              108595000.5
rounded with fix decimal places:    108595000.50
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

try using

value = new BigDecimal(yourString);
doubleValue = value.doubleValue();

if you want the exact value.

If you want 2 numbers after ","

double a = yourDouble; 
System.out.printf("%.2f",a)
cybronele
  • 31
  • 5