944,118 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2998
  • Java RSS
Apr 1st, 2005
0

Number formatting/Help with doubles

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bud4java is offline Offline
13 posts
since Mar 2005
Apr 1st, 2005
0

Re: Number formatting/Help with doubles

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2005
0

Re: Number formatting/Help with doubles

Also, maybe check that the 1000 is a double. Although I don't think that would matter because precision goes up not down.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2005
0

Re: Number formatting/Help with doubles

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bud4java is offline Offline
13 posts
since Mar 2005
Apr 1st, 2005
0

Re: Number formatting/Help with doubles

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 1st, 2005
0

Re: Number formatting/Help with doubles

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bud4java is offline Offline
13 posts
since Mar 2005
Apr 1st, 2005
0

Re: Number formatting/Help with doubles

Yes, you can use the DecimalFormat class to format it to however you want. like this:
Java Syntax (Toggle Plain Text)
  1. double d = .099439823042348209482
  2.  
  3. DecimalFormat dec = new DecimalFormat(###.##); //two decimal places
  4.  
  5. System.out.println("d = " + dec.format(d));
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 2nd, 2005
0

Re: Number formatting/Help with doubles

Quote 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)
  1. double d = .099439823042348209482
  2.  
  3. DecimalFormat dec = new DecimalFormat(###.##); //two decimal places
  4.  
  5. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bud4java is offline Offline
13 posts
since Mar 2005
Apr 2nd, 2005
0

Re: Number formatting/Help with doubles

Glad I could help and glad you got a solution.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Best Java Program Editor?
Next Thread in Java Forum Timeline: How can i call my FileOutput method to output the array ?? really confused...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC