Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Using return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using return" Watch "Using return" New topic
Author

Using return

Steve Lang
Greenhorn

Joined: Feb 11, 2009
Posts: 1
Can you use return and format the output? Currently i have this:

Using return i am supposed to format it to only print two decimal places. Also, nothing is currently printing anyway.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

You are returning a double. A double is just a floating point value. It doesn't contain any formatting.

If you want the output formatted, either format it to a string using the DecimalFormat class, or format it as it is being printed using the printf() method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

Also I dont see a reason for the following code

you can directly return bill without the use of an additional double value. Also in the first case you can directly return bill *0.5. Lastly you can use the ternary operator ?: so your function is reduced to a single line of code.


SCJP, SCWCD.
|Asking Good Questions|
Kieren Dixon
Greenhorn

Joined: Jan 30, 2009
Posts: 27
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Henry Wong wrote:You are returning a double. A double is just a floating point value. It doesn't contain any formatting.

If you want the output formatted, either format it to a string using the DecimalFormat class, or format it as it is being printed using the printf() method.

Henry


Henry, are both your suggestions done at print? Since I am new, I have been messing around with this for practice. The best I could do was call the method in main and manipulate the return there.

Is there a specific way to have the method be called with the return already formatted?

This is what I did at print, also did pretty much the same with printf("$%8.2f", getBillAmount(2100.00));

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

I believe what Henry was saying was that you could either:

a) format the double when you go to print it

or

b) change the method to return a string, and format the double to a string inside the method


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Brian Pianczk wrote:
Is there a specific way to have the method be called with the return already formatted?


Again, a double is just a conceptual number; it does not include any kind of formatting. You can write the number 1/3 as 0.33333333333 or 0.3333 or 3.33x10-1, or many other different ways, but they're all represented by the same double value. Formatting happens only when you print a value.

If you want your method to return a specific formatted display of a number, and you'll never actually do any math on the returned value again, then you could have the method return a String, and put the format call into the method -- i.e.,



... but I wouldn't recommend this. Your original getBillAmount() did one thing: it computed a value. Your new one does two things: computes a value and formats it. In general, you want most of your methods to do just one thing -- that makes the code easier to read, and it's more flexible. If you had a lot of calculation methods and they all formatted their output, and now you decide you need to use a different format, you have to make changes all over the code! If, on the other hand, you did all your formatting and output in one place -- i.e., here, in main() -- then it'd be easy to make the change. So I actually think your original was just fine -- returning the double is a better plan.

Finally, about the advice you got to use the ternary operator: your original method was longer, yes, but it was also nicely self-documenting. The variables "discounted" and "notDiscounted" make the purpose of the code easy to understand. That "ternary operator" has its place, but it's something that should be used sparingly. Here, you've used it to turn a nice, clear, readable method into a terse, unreadable Perl-like blurt. Avoid clever, terse code -- and take all the style advice you get with a grain of salt. Always keep in mind that you, and others, might have to read the code again in a year. Which version would you rather find?

[Jess in Action][AskingGoodQuestions]
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Edit: Question was answered as I was writing it, thanks Ernest!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using return
 
Similar Threads
Variable Shadowing
DecimalFormat is not setting the format for is_IS Language
Is it possible to reformat the output from a double value?
format double
Jtable double with 2 decimal and data validation