Results 1 to 10 of 10
Thread: String to double errors
- 12-03-2010, 09:52 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
String to double errors
Hi, I am trying to cut down the decimals to two, but I get this error(see picture).
I have try several methods but getting the same errors.
// Patrik
Java Code:import java.text.DecimalFormat; public class TestD { public TestD(){ System.out.println("Double value: " + getArea()); } public double getArea() { double dTemp = Math.PI; DecimalFormat twoDForm = new DecimalFormat("#.##"); String aTemp = twoDForm.format(dTemp); return Double.valueOf(aTemp.trim()).doubleValue(); } }
- 12-03-2010, 09:59 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
I runs fine for me; sorry, that thumbnail is too small for me; what is it protesting about? Is "3.14" (with a dot) or "3,14" (with a comma).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-03-2010, 10:12 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
I am using BlueJ.
Here is the error:
java.lang.NumberFormatException: For input string: "3,14"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
at java.lang.Double.valueOf(Double.java:475)
at TestD.getArea(TestD.java:30)
at TestD.<init>(TestD.java:19)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
at bluej.runtime.ExecServer$3.run(ExecServer.java:790 )
- 12-03-2010, 10:28 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-03-2010, 10:41 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
Thanks it is solved now :-)
- 12-04-2010, 06:32 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
More problem I need help for. This is nearly the same code with two extra variables.
I get this error message:Java Code:import java.text.DecimalFormat; public class TestD { int width; int height; public TestD(){ width = 20; height = 30; System.out.println("Double value: " + getArea(width, height)); } public double getArea(int width, int height) { double dTemp = Math.PI * (double)width * (double)height; DecimalFormat twoDForm = new DecimalFormat("#,##"); String aTemp = twoDForm.format(dTemp); return Double.valueOf(aTemp.trim()).doubleValue(); } }
It is complaining on the last row of the code with the return.Java Code:[COLOR="DarkRed"]java.lang.NumberFormatException: For input string: "18 85" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224) at java.lang.Double.valueOf(Double.java:475) at TestD.getArea(TestD.java:29) at TestD.<init>(TestD.java:14) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at bluej.runtime.ExecServer$3.run(ExecServer.java:790)[/COLOR]
Anyone who can help me with this?
-
In your current code, your DecimalFormat object is superfluous and doing nothing but helping to cause an error. If the method takes in two numbers and then returns a number (a double), why convert it to a String? If however your goal is to learn to use a DecimalFormat object to convert a double to a String and then back again, then you'll want to use DecimalFormat's parse method as well:
Java Code:import java.text.DecimalFormat; import java.text.ParseException; public class TestD { int width; int height; public TestD() throws ParseException { width = 20; height = 30; System.out.println("Double value: " + getArea(width, height)); } public double getArea(int width, int height) throws ParseException { double dTemp = Math.PI * (double) width * (double) height; DecimalFormat twoDForm = new DecimalFormat("#,##"); String aTemp = twoDForm.format(dTemp); System.out.println("aTemp is " + aTemp); //!!return Double.valueOf(aTemp.trim()).doubleValue(); return twoDForm.parse(aTemp).doubleValue(); } public static void main(String[] args) { try { new TestD(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
- 12-05-2010, 12:40 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
Hi, I did run your code and got no errors :-)
But, I am still not get the double value with two decimals. Actually I do not care if I do need to convert it to a string and then back to double.
It was just one solution I found on the net. (I do not know if this could be done different?)
The main reson is to just get a double value with only two decimals.
// Patrik
-
The method is supposed to return a double, and doubles really don't have a number of decimals -- rather a String representation of a double does. So don't worry about the number of decimals of the number until you want to display the String.
You should worry about this as it messes with your result for no reason at all and can introduce errors.Actually I do not care if I do need to convert it to a string and then back to double.
Again, use a DecimalFormat to create a String from the double that has two decimals, but again only do this when you want to display the double as a String.The main reson is to just get a double value with only two decimals.
- 12-05-2010, 08:42 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
parse string into 2 double variables
By woodsie2523 in forum New To JavaReplies: 3Last Post: 10-26-2010, 01:46 PM -
convert String to Double
By azurovyhrosik in forum CLDC and MIDPReplies: 5Last Post: 10-22-2008, 02:46 AM -
NullPointerException converting String to double
By infaddict in forum New To JavaReplies: 3Last Post: 07-19-2008, 06:01 PM -
Converting String to Double
By srini in forum New To JavaReplies: 1Last Post: 12-24-2007, 08:03 PM -
convert string to a double?
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-27-2007, 03:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks