| | |
Number formatting/Help with doubles
![]() |
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
Greetings all...long time reader, first time poster.
I have 2 java files, one that contains methods that do conversions, the other contains a loop that allows a user to select "what" they want to convert. The program works, but one of the conversions is to multiply by 0.0092. When I do a test of input data (I usually start with 1000) instead of getting 9.2, I get 9. Lower numbers like 100 return 0.0 instead of 0.92.
Both the input and the return statements indicate "double", so is there a way to get more precise with the return double?
Also, I saw something in my text book that sort of did this, but through a print line method. (this is from memory, I don't have my book with me) It was something like:
System.out.printf(%5.2 yada yada yada)
So I'm wondering if there's a way to get that kind of format without incorporating it in a print statement.
Thanks for any consideration and/or help.
Bud
I have 2 java files, one that contains methods that do conversions, the other contains a loop that allows a user to select "what" they want to convert. The program works, but one of the conversions is to multiply by 0.0092. When I do a test of input data (I usually start with 1000) instead of getting 9.2, I get 9. Lower numbers like 100 return 0.0 instead of 0.92.
Both the input and the return statements indicate "double", so is there a way to get more precise with the return double?
Also, I saw something in my text book that sort of did this, but through a print line method. (this is from memory, I don't have my book with me) It was something like:
System.out.printf(%5.2 yada yada yada)
So I'm wondering if there's a way to get that kind of format without incorporating it in a print statement.
Thanks for any consideration and/or help.
Bud
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
From the file with the control loop:
else if (option == 3) {
System.out.println("Enter amount of Things:");
amount = Double.parseDouble(stdin.readLine());
gift.enterThings(amount);
}
From the file with the methods:
public void enterThings(double amount) {
things = amount * 0.0092;
totalCollected += things;
} // end method
Thanks for your consideration.
Bud
else if (option == 3) {
System.out.println("Enter amount of Things:");
amount = Double.parseDouble(stdin.readLine());
gift.enterThings(amount);
}
From the file with the methods:
public void enterThings(double amount) {
things = amount * 0.0092;
totalCollected += things;
} // end method
Thanks for your consideration.
Bud
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
First make sure that variable "things" is a double, and then with the .0092 value, you might have to procede it with a d, or f....ex: .0092d or .0092f, try both of those. If that doesn't work, then create an instance variable that is a double value containing .0092 like this: double value = .0092 and then substitute value in where you have .0092, and that should work. If any of that doesn't work post a little more code, and I will see what I can do.
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by server_crash
First make sure that variable "things" is a double, and then with the .0092 value, you might have to procede it with a d, or f....ex: .0092d or .0092f, try both of those. If that doesn't work, then create an instance variable that is a double value containing .0092 like this: double value = .0092 and then substitute value in where you have .0092, and that should work. If any of that doesn't work post a little more code, and I will see what I can do.
Grrr...get ready to laugh.
"things" was a double, and initalized. So was "amount". HOWEVER, after further review, the object that returned the value to the user (totalCollected) was initalized as an INT!!! D'oh! Of course, Java did exactly what I told it to do, not what I meant it to do. Then again, I was working on this for HOURS at a time, (late at night, and early in the morning) so my brain wasn't fully focused. But when I looked at it a few minutes ago, the fault was SO OBVIOUS!
This does bring up the next challenge. When I enter 100 and it multiplies by .0092, the result is now 0.919999999999999999. Is there a way (like in Excel where you can format decimals to round to currency to be .92?)
Thanks for your help & consideration.
Bud
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Yes, you can use the DecimalFormat class to format it to however you want. like this:
Java Syntax (Toggle Plain Text)
double d = .099439823042348209482 DecimalFormat dec = new DecimalFormat(###.##); //two decimal places System.out.println("d = " + dec.format(d));
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by server_crash
Yes, you can use the DecimalFormat class to format it to however you want. like this:
Java Syntax (Toggle Plain Text)
double d = .099439823042348209482 DecimalFormat dec = new DecimalFormat(###.##); //two decimal places System.out.println("d = " + dec.format(d));
Crash,
Here is a portion of the code that I ended up using that actually got the double formatted the way I wanted it:
System.out.printf("%5.2f",
Thanks for your help.
Bud
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Best Java Program Editor?
- Next Thread: turning an image to binary code
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class client code compile compiler component converter database developmenthelp eclipse error fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html ide image input int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying number online problem program programming project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






