Parsing Scientific Notation from String
I am trying to figure out how to parse a number into a double which represented as a String in scientific notation.
For example, I have the number: 8.86162677393184E-02.
When I do a Double.parseDouble(String) call on it, I get this error:
Quote:
java.lang.NumberFormatException: For input string: "E-2"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at RunDB.displayResults(RunDB.java:208)
at RunDB.executeChangeQuery(RunDB.java:151)
at RunDB.<init>(RunDB.java:55)
at CPLFProgram.main(CPLFProgram.java:17)
Can anyone help me out?
Re: Parsing Scientific Notation from String
See the DecimalFormat class. You can create the class with a specified format, then parse into a number. Give the code below a shot
Code :
String file = "8.86162677393184E-02";
try{
DecimalFormat format = new DecimalFormat("#.##");
System.out.println(format.parse(file).floatValue());
}catch(Exception e){
e.printStackTrace();
}
Re: Parsing Scientific Notation from String
Nope, failed.
This is the error now:
Quote:
java.text.ParseException: Unparseable number: "E-2"
at java.text.NumberFormat.parse(NumberFormat.java:333 )
at RunDB.executeChangeQuery(RunDB.java:209)
at RunDB.<init>(RunDB.java:57)
at CPLFProgram.main(CPLFProgram.java:17)
Re: Parsing Scientific Notation from String
Hmmm, worked for me. Is the exception being thrown with the above number, or something else (post the something else if that is the case)? You may have to fiddle with the DecimalFormat object a bit, but it should be doable.
Re: Parsing Scientific Notation from String
Yep, the number above and about a thousand other numbers.
What strikes me as odd is I think the 0 between the - and the 2 is just being thrown away (possibly the cause of the exception).
Re: Parsing Scientific Notation from String
In java, a blank space between the exponent and the base or power are not allowed. For example, these are not allowed: (assume under-scores represent spaces)
1.23_e5
1.23e_5
1.23e_-5
1.23e-_5
1.23e_-_5
check your data and make sure that there are indeed no blank spaces (or any other characters for that matter)
Re: Parsing Scientific Notation from String
Ok, so what the hell happened to the 0 when I told it to parse the number?
Re: Parsing Scientific Notation from String
Ok, this has translated into a database issue that I have no idea what to do.
I am retrieving these numbers from a database. As it turns out, when it reads in the values from the database, it is not reading in the whole number, just the E-2. Is there any way to fix this?
I am reading in the number with the ResultSet.getString(int) method and attempting to parse it. I have tried using the ResultSet.getBigDecimal(int) method, but that throws the following error:
Code java:
java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:505)
at java.math.BigDecimal.<init>(BigDecimal.java:728)
at sun.jdbc.odbc.JdbcOdbcResultSet.getBigDecimal(JdbcOdbcResultSet.java:1957)
at RunDB.executeChangeQuery(RunDB.java:206)
at RunDB.<init>(RunDB.java:58)
at CPLFProgram.main(CPLFProgram.java:17)
Since the problem has actually merged with my other current issues, I'm going to cross-post this with the topic I created for the database a few days ago: http://www.javaprogrammingforums.com...html#post23158