• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to use printf %f with a function?

 
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and thanks in advance,

I can't figure out how to get my output formatted the way I would like. I tried to find info online and it wasn't exactly what I was looking for. Here is my code below. The point being to return a tip amount. The code works, my only problem is it returned four decimals places instead of two. So, I tried to format it, but now I'm getting errors.



 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think formatting numbers like that is only allowed when you use printf, you are using println
 
Saloon Keeper
Posts: 9539
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you change println() to printf() the formatting will work but you'll loose the ending new-line. You can achieve a new-line with printf() by adding "%n" anywhere in the format string, e.g. the end.
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah! and I thought I had typed in printf. I guess I just needed a break from coding. Thanks again.
 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use %1.2f. That means one character with two after the decimal point. If you want to print 123.45 and you are sure you aren't going to have any negative numbers, you want three digits before the decimal point, one decimal point and two after, making six: %6.2f
Also don't use doubles for money. Either denominate alll amounts in ¢ and use integer arithmetic, or use BigDecimal.
 
Sheriff
Posts: 17065
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also don't use doubles for money. Either denominate alll amounts in ¢ and use integer arithmetic, or use BigDecimal


Using a double in this case isn't going to hurt. It's only when you have multiple (in the hundreds or more) that you're going to have to worry about the cumulative effects of roundoff errors inherent to floating point operations.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic