• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using return

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit: Question was answered as I was writing it, thanks Ernest!
 
What's wrong? Where are you going? Stop! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic