User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Mar 2005
Posts: 13
Reputation: Bud4java is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Bud4java Bud4java is offline Offline
Newbie Poster

Number formatting/Help with doubles

  #1  
Apr 1st, 2005
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Number formatting/Help with doubles

  #2  
Apr 1st, 2005
Please post some code, if you are using doubles it should show up as a double. Other than that, you can use the DecimalFormat class to format decimals to a specific decimal place.
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Number formatting/Help with doubles

  #3  
Apr 1st, 2005
Also, maybe check that the 1000 is a double. Although I don't think that would matter because precision goes up not down.
Reply With Quote  
Join Date: Mar 2005
Posts: 13
Reputation: Bud4java is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Bud4java Bud4java is offline Offline
Newbie Poster

Re: Number formatting/Help with doubles

  #4  
Apr 1st, 2005
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
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Number formatting/Help with doubles

  #5  
Apr 1st, 2005
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.
Reply With Quote  
Join Date: Mar 2005
Posts: 13
Reputation: Bud4java is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Bud4java Bud4java is offline Offline
Newbie Poster

Re: Number formatting/Help with doubles

  #6  
Apr 1st, 2005
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
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Number formatting/Help with doubles

  #7  
Apr 1st, 2005
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));
Reply With Quote  
Join Date: Mar 2005
Posts: 13
Reputation: Bud4java is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Bud4java Bud4java is offline Offline
Newbie Poster

Re: Number formatting/Help with doubles

  #8  
Apr 2nd, 2005
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
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Number formatting/Help with doubles

  #9  
Apr 2nd, 2005
Glad I could help and glad you got a solution.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:18 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC