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

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

    807601
      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){
            //Ask for input
               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;            
                        df.format(answer);
                        JOptionPane.showMessageDialog(null, inputString + " = " + (answer));
                        break;
                     case '-':
                        //DecimalFormat df = new DecimalFormat("#.#");             
                        double answer2 = n1 + -n2;            
                        df.format(answer2); 
                        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;            
                           df.format(answer3);              
                           JOptionPane.showMessageDialog(null, inputString + " = " + (answer3));
                        }            
                        break;
                            
                     case '*':
                        //DecimalFormat df = new DecimalFormat("#.#");             
                        double answer4 = n1 * n2;            
                        df.format(answer4);            
                        JOptionPane.showMessageDialog(null, inputString + " = " + (answer4));
                        break;
                     case '%':
                        //DecimalFormat df = new DecimalFormat("#.#");             
                        double answer5 = n1 % n2;            
                        df.format(answer5);          
                        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!