Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with the output  RSS feed

 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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

 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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().
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at NumberFormat
 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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: 560
9
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
 
Ebenezer Y Teah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the suggestion is to replace this line:

with this line:
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!