3 Replies Latest reply: May 2, 2008 3:57 PM by 807601

# Can't get it to round to 2 decimals, using DecimalFormat, please proof read

Why won't this output #.##? No matter how I change them it still gives me exact answers.
``````   import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.DecimalFormat;

public class Calculator{
public static void main (String[] args){
String inputString = JOptionPane.showInputDialog("Input your desired calculation, separating numbers and operation with spaces.(+-/*%)");
//split input to make switch function
String[] result = inputString.split("\\s");

/*I Put both of these in here, but stuck with the Double,
I think thats the better fit*/

//float n1 = Float.parseFloat(result[0]);
//float n2 = Float.parseFloat(result[2]);
double n1 = Double.parseDouble(result[0]);
double n2 = Double.parseDouble(result[2]);
char c = result [1].charAt(0);

if((c == '+')  || (c == '/') || (c == '*') || (c == '%') || (c == '-'))
{

DecimalFormat df = new DecimalFormat("#.##");

//switch function to cover the 5 functions, and divide by zero message
switch(c)
{
case '+':

double answer = n1 + n2;
JOptionPane.showMessageDialog(null, inputString + " = " + (answer));
break;
case '-':
//DecimalFormat df = new DecimalFormat("#.#");
double answer2 = n1 + -n2;
JOptionPane.showMessageDialog(null, inputString + " = " + (answer2));
break;
case '/':
if(n2 == 0)
JOptionPane.showMessageDialog(null, "You know that's a no no!");
else
{
//DecimalFormat df = new DecimalFormat("#.#");
double answer3 = n1 / n2;
JOptionPane.showMessageDialog(null, inputString + " = " + (answer3));
}
break;

case '*':
//DecimalFormat df = new DecimalFormat("#.#");
double answer4 = n1 * n2;
JOptionPane.showMessageDialog(null, inputString + " = " + (answer4));
break;
case '%':
//DecimalFormat df = new DecimalFormat("#.#");
double answer5 = n1 % n2;
JOptionPane.showMessageDialog(null, inputString + " = " + (answer5));
break;
}
}

else
{
System.out.println("Can't do that operation!");
}

}
}``````
I commented out some other stuff I was thinking on, so just ignore. Thanks!
• ###### 1. Re: Can't get it to round to 2 decimals, using DecimalFormat, please proof
cuz you are not using the value returned by the DecimalFormat object.
``````                  df.format(answer2);
JOptionPane.showMessageDialog(null, inputString + " = " + (answer2));``````
DecimalFormat.format() returns a string. By what magic do you think the primitive double answer2 is going to be changed to a String object?
``````                  String a2 = df.format(answer2);
JOptionPane.showMessageDialog(null, inputString + " = " + (a2));``````
• ###### 2. Re: Can't get it to round to 2 decimals, using DecimalFormat, please proof
No matter how I change them it still gives me exact answers.
decimalFormat changes nothing, it displays something in a prescribed format, so
``````df.format(answer);
JOptionPane.showMessageDialog(null, inputString + " = " + (answer));``````
should be
``````//df.format(answer);
JOptionPane.showMessageDialog(null, inputString + " = " + df.format(answer));``````
• ###### 3. Re: Can't get it to round to 2 decimals, using DecimalFormat, please proof read
Thanks for the help guys!