Existing String to Double

后端 未结 4 1442
醉话见心
醉话见心 2021-01-28 13:54

How can I change existing string into double. I have code like this declared as string but in reality its getting number from the database.. I was doing sting to number conversi

相关标签:
4条回答
  • 2021-01-28 14:25

    Double is immutable and must be constructed with a value (there is not a no-arg constructor): http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html

    0 讨论(0)
  • 2021-01-28 14:29

    Just initialize it in your declaration, and remove the line where you set it to a new object. Take a look at this.

    0 讨论(0)
  • 2021-01-28 14:44
    try {
      double d = Double.parseDouble(str);
      Double D = new Double(d);
    }
    catch( NumberFormatException e ) {
       // input cleansing
       // thou shalt not fail silently
    }
    

    Reference:

    Double.parseDouble

    0 讨论(0)
  • 2021-01-28 14:45

    I don't quite get your problem, but here are a few notes:

    • you can create a double by simple declaring double d = 0. Or new Double(0) (passing the double value as argument)
    • if you want to convert from string to double, use Double.parseDouble(string)
    0 讨论(0)
提交回复
热议问题