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]

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
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);
}
}
> 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.