Having some trouble rounding decimal place

Hello all,

I am having some issues rounding a decimal in my code. My program is running and doing what I would like otherwise. Here is what I have so far:

publicclass MortCalc2

{

publicstaticvoid main(String [] args)

{

//Declare and initialize three variables

double numloanamt = 200000.00;//loan amount of 200k

double numinterest = 0.0575;//interest rate of 5.75%

int numtermyrs = 30;//term of 30 years

int numtermmths = 360;//term of 360 months

double numwholeamt = 545000.00;//loan amount + interest

double numpayment = 0;//monthly payment

numpayment = numwholeamt/numtermmths;//payment equals loan amount plus interest divided by the total term in months

System.out.println("Your monthly payment is $" + numpayment);

}

}

I am getting the results I want with the calculation except they are coming out as 1513.888888888889. I would like to see 1513.89. Can someone point me in the right direction. Thank you in advance for any help you can provide.

Thanks,

Seawall

[1838 byte] By [seawalla] at [2007-10-2 23:36:24]
# 1
use floatinstead ofdouble
kikemellya at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 2
sorry ignore that. try here should do the trick. http://forum.java.sun.com/thread.jspa?threadID=475442&messageID=2204935
kikemellya at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 3

in your import statements use:

import java.text.*;

this allows you to access the DecimalFormat class already implemented.

then at the top of your class define a new variable as

DecimalFormat twoPlaces = new DecimalFormat("#,##00.00");

now whenever you want to format a value to 2 points after the decimal you can just say twoPlaces.format(value)....

at least thats what i always do

djmd02a at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 4
Should I use[code}float[/code]on all or just thenumpayment
seawalla at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 5
Should I usefloaton all or just thenumpayment
seawalla at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 6

Hi, sorry for the first post.

this works okay.

import java.math.*;

public class MortCalc2

{

public static void main(String [] args){

//Declare and initialize three variables

double numloanamt = 200000.00; //loan amount of 200k

double numinterest = 0.0575; //interest rate of 5.75%

int numtermyrs = 30; //term of 30 years

int numtermmths = 360; //term of 360 months

double numwholeamt = 545000.00; //loan amount + interest

double numpayment = 0; //monthly paymen

numpayment = numwholeamt/numtermmths; //payment equals loan amount plus interest divided by the total term in months

numpayment = Math.round(numpayment*100.0) / 100.0;

System.out.println("Your monthly payment is $" + numpayment);

}

}

kikemellya at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 7

> Hi, sorry for the first post.

>

> this works okay.

>

> > import java.math.*;

> public class MortCalc2

> {

> public static void main(String [] args){

>

>

> are and initialize three variables

> double numloanamt = 200000.00; //loan amount of

> of 200k

> double numinterest = 0.0575; //interest rate of

> of 5.75%

> int numtermyrs = 30; //term of 30 years

> int numtermmths = 360; //term of 360 months

> double numwholeamt = 545000.00; //loan amount +

> + interest

> double numpayment = 0; //monthly paymen

>

> numpayment = numwholeamt/numtermmths; //payment

> t equals loan amount plus interest divided by the

> total term in months

> numpayment =

> Math.round(numpayment*100.0) / 100.0;

> System.out.println("Your monthly payment is $" +

> + numpayment);

> }

> }

>

Thats right, use an Irish screwdriver (a hammer) instead of the correct solution as posted by djmd02 in repsonse #3.

sabre150a at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 8
I used the solution from djmd02 and it worked great. Thank you very much. I really appreciate this.Seawall
seawalla at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 9
HA irish screwdriver....hey no problem bud
djmd02a at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 10
> DecimalFormat twoPlaces = new DecimalFormat("#,##00.00");Better to use "#,###.00" as your pattern. The above pattern will give a leading zero for numbers less than 10.
floundera at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...
# 11
yeah sorry i wasn't really payin attention good call on that one flounder...but all in all glad i could help
djmd02a at 2007-7-14 16:18:46 > top of Java-index,Java Essentials,New To Java...