Problems getting floating point number to show in GUI...
Hello! So I'm trying to get the very last part of this code to show up as "The total accumulation is: $(whateverMynumberEquals)" in the GUI. The problem is that I'm using a floating point and I have no clue how to put it in proper form so that it shows up correctly. I put what I THOUGHT it would be below, so you can tell me what's right and what's not. Or should I make it a variable and THEN put it in the GUI? I just need the box to show the number with two decimal points only. Anyways, as you can see, I don't know what I'm doing with it, so please help! Thanks!
Code Java:
import java.util.Scanner;
import javax.swing.JOptionPane;
public class CompoundInterest {
/**
* @param args
*/
public static void main(String[] args) {
Scanner QWERTY = new Scanner(System.in);
JOptionPane.showMessageDialog(null, "Welcome to the Compound Interest Calculator!");
JOptionPane.showMessageDialog(null, "We are going to ask you for certain information in order calculate your interest!");
System.out.println();
double P = 0;
P = Double.parseDouble(
JOptionPane.showInputDialog("What is the initial deposit (principle)? Numbers only please!"));
JOptionPane.showMessageDialog(null, "The value entered is: $" + P);
double r = 0;
r = Double.parseDouble(
JOptionPane.showInputDialog("What is the annual interest rate in percent? [xx.yy]"));
JOptionPane.showMessageDialog(null, "The value entered is: " + r);
int q = 0;
q = Integer.parseInt(
JOptionPane.showInputDialog("What is the number of times the compounding is done per year?"));
JOptionPane.showMessageDialog(null, "The value entered is: " + q);
double n = 0;
n = Double.parseDouble(
JOptionPane.showInputDialog("How many years is the principle left in deposit?"));
JOptionPane.showMessageDialog(null, "The value entered is: " + n);
r = (r/100);
double exponent = Math.pow(1 + r/q, n*q);
double A = 0;
A = P * exponent;
JOptionPane.showMessageDialog(null, "The total accumulation is:" +
System.out.printf("$%.2f\n", A));
}
}
Re: Problems getting floating point number to show in GUI...
Don't use System.out.printf() as that method prints things to the console.
Instead look in the String API docs for a method that formats things the same way printf() does, but which returns a String.