# formatting decimal spaces

Brock Barnes

Greenhorn

Posts: 20

posted 14 years ago

Greetings all,

I'm a greenhorn, and I need a little help. I had a little free time at work today and was trying to come up with some example script ideas for practice, and after hearing my wife complain about the credit card bills lastnight, I came up with the one below. Everything in the example works like I wanted/expected, except for the formatting of the double currentTotal. I'm not exactly sure yet how to format the output to where it only displays 2 places after the decimal: $219.79 vs. $219.79255898393723. I've included my sample code and output below (please forgive me...output is long). Any suggestions? Thanks in advance! -Brock

/**********************************************************

* This example should take the total of the credit card,

* add the monthly interest rate to the total and display

* how much you owe at the time of making a payment.

* Then it should subtract the payment and show how much

* you owe after the payment. It should repeat itself until

* you no longer owe anything. It will also show how many

* total months it takes to pay the darn thing off. That is

* provided you don't put anything else on the card like

* dinner at Chili's or Applebee's. =)

***********************************************************/

/* here is the output I get:

C:\jfiles>java CreditCard

Month 1

before payment = $5079.0

after payment = $4954.0

Month 2

before payment = $5032.2732

after payment = $4907.2732

Month 3

before payment = $4984.80811656

after payment = $4859.80811656

Month 4

before payment = $4936.593084801648

after payment = $4811.593084801648

Month 5

before payment = $4887.616255541514

after payment = $4762.616255541514

Month 6

before payment = $4837.86559237907

after payment = $4712.86559237907

Month 7

before payment = $4787.328868738659

after payment = $4662.328868738659

Month 8

before payment = $4735.99366486473

after payment = $4610.99366486473

Month 9

before payment = $4683.847364769593

after payment = $4558.847364769593

Month 10

before payment = $4630.877153132952

after payment = $4505.877153132952

Month 11

before payment = $4577.070012152452

after payment = $4452.070012152452

Month 12

before payment = $4522.412718344461

after payment = $4397.412718344461

*/

edited by Dave to format the code and delete some of the results

[ May 29, 2002: Message edited by: Dave Vick ]

I'm a greenhorn, and I need a little help. I had a little free time at work today and was trying to come up with some example script ideas for practice, and after hearing my wife complain about the credit card bills lastnight, I came up with the one below. Everything in the example works like I wanted/expected, except for the formatting of the double currentTotal. I'm not exactly sure yet how to format the output to where it only displays 2 places after the decimal: $219.79 vs. $219.79255898393723. I've included my sample code and output below (please forgive me...output is long). Any suggestions? Thanks in advance! -Brock

/**********************************************************

* This example should take the total of the credit card,

* add the monthly interest rate to the total and display

* how much you owe at the time of making a payment.

* Then it should subtract the payment and show how much

* you owe after the payment. It should repeat itself until

* you no longer owe anything. It will also show how many

* total months it takes to pay the darn thing off. That is

* provided you don't put anything else on the card like

* dinner at Chili's or Applebee's. =)

***********************************************************/

/* here is the output I get:

C:\jfiles>java CreditCard

Month 1

before payment = $5079.0

after payment = $4954.0

Month 2

before payment = $5032.2732

after payment = $4907.2732

Month 3

before payment = $4984.80811656

after payment = $4859.80811656

Month 4

before payment = $4936.593084801648

after payment = $4811.593084801648

Month 5

before payment = $4887.616255541514

after payment = $4762.616255541514

Month 6

before payment = $4837.86559237907

after payment = $4712.86559237907

Month 7

before payment = $4787.328868738659

after payment = $4662.328868738659

Month 8

before payment = $4735.99366486473

after payment = $4610.99366486473

Month 9

before payment = $4683.847364769593

after payment = $4558.847364769593

Month 10

before payment = $4630.877153132952

after payment = $4505.877153132952

Month 11

before payment = $4577.070012152452

after payment = $4452.070012152452

Month 12

before payment = $4522.412718344461

after payment = $4397.412718344461

*/

edited by Dave to format the code and delete some of the results

[ May 29, 2002: Message edited by: Dave Vick ]

Dave Vick

Ranch Hand

Posts: 3244

posted 14 years ago

Brock

What you want to look at is the NumberFormat class and its getCurrencyInstance and format methods. You'll get an instance of a NumberFormat object with the getCurrentcyInstance method and then use it to call the format method and pass in the number to be formatted (it is overloaded and can take multiple types of arguments), it'll then return a String with the formatted number.

I added the UBB tags to your post to format your code and also got rid of the other 50 months of results you posted - we got the idea after the first 12

hope that helps

What you want to look at is the NumberFormat class and its getCurrencyInstance and format methods. You'll get an instance of a NumberFormat object with the getCurrentcyInstance method and then use it to call the format method and pass in the number to be formatted (it is overloaded and can take multiple types of arguments), it'll then return a String with the formatted number.

I added the UBB tags to your post to format your code and also got rid of the other 50 months of results you posted - we got the idea after the first 12

hope that helps

Dave

Brock Barnes

Greenhorn

Posts: 20

Brock Barnes

Greenhorn

Posts: 20