I just started doing
java and one of my assignments require the format for the percent to be displayed but yet when I format it to display as a percent it reverts to just a nondecimal. What am I doing wrong. I have it print prior to format and all looks good(.0525) but when applying the format it shows 5% where is the 5.25%
Anyone?
Thanks
package project3_1;
import java.util.Scanner;
import java.text.NumberFormat;
import java.math.*;
public class Project3_1 {
public static void main(
String[] args) {
// Create a Scanner Object and While Loop
Scanner sc = new Scanner(System.in);
String choice ="y";
while (choice.equalsIgnoreCase("y"))
{
// Get Input from user
System.out.print("Enter Loan Amount: ");
double loanamount = sc.nextDouble();
System.out.print("Enter Interest Rate: ");
double interestrate = sc.nextDouble();
System.out.println(interestrate); //test print remove later
// Calculate Interest Amount
BigDecimal decimalinterestrate = new BigDecimal(interestrate);
decimalinterestrate = decimalinterestrate.setScale(3, RoundingMode.HALF_UP);
double interestamount = loanamount * interestrate;
System.out.println(decimalinterestrate); //
test print remove later
// Print out data
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String message =
"Loan Amoount: " + currency.format(loanamount) + "\n"
+ "Interest rate: " + percent.format(decimalinterestrate)+"\n"
+ "Interest: " + currency.format(interestamount) + "\n";
System.out.println(message);
// See if they want to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}