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:

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

## All 8 Replies

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.

Also, maybe check that the 1000 is a double. Although I don't think that would matter because precision goes up not down.

From the file with the control loop:

``````else if (option == 3) {
System.out.println("Enter amount of Things:");
}
``````

From the file with the methods:

``````public void enterThings(double amount) {
things = amount * 0.0092;
totalCollected  += things;
} // end method
``````

Bud

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.

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.

"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

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));``````

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",