• 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

Problem with the output

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did compile this code and it work, but the problem is, the output that is being displayed shows many numbers after the decimal point instead of just two. What must I do.
Here is the output and the code:
Output:
Please enter the amount of your Capital.
100
Please enter the number of payments to pay back capital amount
3
Please enter the annual percentage rate as a decimal. Ex. 3.5 = 0.035
0
Your capital amount is 100.0
0.0
3
33.333333333333336

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use System.out.printf(...) and specify the number of decimal places. Example:

printf() is very powerful but takes some getting used to and some reading.

If you don't want to print but just get a formatted String you can use something like:

It uses the same formatting string syntax as printf().
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at NumberFormat
 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but when use the system.out.printf it thorws an exception as:
Please enter the amount of your Capital.
100
Please enter the number of payments to pay back capital amount
3

 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your code that causes the error?
(I think Carey Brown's answer has a typo - it should be

 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did, but it throws in an exception as well:
Please enter the amount of your Capital.
100
Please enter the number of payments to pay back capital amount
3
 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying that I should replaced this line:
with the code that you provided as It continues to throw exception.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ebenezer Y Teah, welcome to the Ranch.

Please always suround your code with code tags. You can simply select your all code and click "Code" button in the text editing panel.
Also, before you submit your post, please click "Preview" button instead of "Submit" right away, so you'd see how your post would look like after you submit it. Thank you.
I have added code tags for you this time, see how better your code looks?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:What is your code that causes the error?
(I think Carey Brown's answer has a typo - it should be


%n is the platform independent new-line
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ebenezer Y Teah wrote:Are you saying that I should replaced this line:
with the code that you provided as It continues to throw exception.


I wasn't suggesting to replace that line. In your original post you were objecting to the output of
33.333333333333336

Regarding your exception
Dollar amount is Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%2f'

printf() takes a variable number of comma separated arguments. The first argument is the format String. Other optional arguments have to match the type and order of the '%' formatters. "%f" formats a floating point. Changing it to %.2f (don't miss the decimal point) says to always output 2 decimal places. As I said, some reading of the Javadocs may be needed.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the suggestion is to replace this line:

with this line:
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I believe the suggestion is to replace this line...with...


It should perhaps be mentioned that when dealing with currency (or any absolute values), you're far better off using BigDecimal, rather than double.

Specifically: In your given example, which would appear to be a "repayment" schedule that could (presumably) span many years and hundreds of payments, ANY rounding could leave a substantial residue (or overpayment) after the last payment. At least with BigDecimal, you'll know what that is.

Winston
 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the hint which was helpful.How can I do a reverse, to have APR, capital amount and the number of payments after compiling the initial codes.
The initial compilation displays:
Please enter the amount of your Capital.
100
Please enter the number of payments to pay back capital amount
3
Please enter the annual percentage rate as a decimal. Ex. 3.5 = 0.035
0
Your capital amount is 100.0
0.0
3
33.33
I actually want:
APR, capital amount and the number of payments to be lines 1,2 and 3 when compile.
Thanks.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you say you want something like "Monthly payment: $33.33" to print? That's easy. In the format string (the first parameter) of printf, anything that's not prefixed with a "%" is just printed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic