•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,590 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,651 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1828 | Replies: 8
![]() |
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Rep Power: 4
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: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Rep Power: 4
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
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
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:
Rep Power: 4
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
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
Yes, you can use the DecimalFormat class to format it to however you want. like this:
double d = .099439823042348209482
DecimalFormat dec = new DecimalFormat(###.##); //two decimal places
System.out.println("d = " + dec.format(d));•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
Originally Posted by server_crash
Yes, you can use the DecimalFormat class to format it to however you want. like this:
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
Other Threads in the Java Forum
- Previous Thread: Best Java Program Editor?
- Next Thread: turning an image to binary code



Linear Mode