Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Decimal formatting?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Decimal formatting?

    my program is supposed to include The three sides of the triangle formatted to two decimal places The perimeter formatted to one decimal place The area formatted to one decimal place The unformatted area. It does run, but it is not decimal formatted. I have read my book to try and figure out how to do this, but it doesn't make since to me. Can someone tell me how I would do this?

     
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.text.DecimalFormat;
    import java.util.StringTokenizer;
     
    public class ShelbyHarms_3_03 {
       public static void main (String [] args) {
       double a, b, c; //Input sides of triangle
       double x; //Perimeter of triangle
       double area; //Area of triangle
       String inputStr;
       StringTokenizer st;
     
     
       //Enter sides of the triangle separated by spaces
       inputStr = JOptionPane.showInputDialog("Enter sides of the triangle separated by spaces");
       st = new StringTokenizer(inputStr);
       a = Double.parseDouble(st.nextToken());
       b = Double.parseDouble(st.nextToken());
       c = Double.parseDouble(st.nextToken());
     
     
       DecimalFormat formatter = new DecimalFormat("0.00");
     
       JOptionPane.showMessageDialog(null,"Your three triangle lengths are: " + '\n' + a +
               '\n' + b + '\n' + c);
     
       JOptionPane.showMessageDialog(null,"The area of your triangle is: " +
               ShelbyHarms302.triangleArea(a, b, c));
     
        }
     
        public static double triangleArea (double a, double b, double c) {
          double s = (a + b + c)/2.0;
          double x = ((s) * (s-a) * (s-b) * (s-c)); 
          double area = Math.sqrt(x);
          return area;     
     
        } // End main()
    } // End class


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Decimal formatting?

    I see in your code you create a DecimalFormat object. I don't see where you use it!
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Decimal formatting?

    Quote Originally Posted by Junky View Post
    I see in your code you create a DecimalFormat object. I don't see where you use it!
    I think that's my problem. I don't understand how to use it in this code.

    --- Update ---

    Quote Originally Posted by Junky View Post
    I see in your code you create a DecimalFormat object. I don't see where you use it!
    would it be something like this?

       JOptionPane.showMessageDialog(null, formatter.format(a, b, c));
       JOptionPane.showMessageDialog(null, formatter.format(s));
       JOptionPane.showMessageDialog(null, formatter.format(area));


    --- Update ---

    Never mind figured it out. I'm still pretty new to this. I am just wondering now how I might get the The three sides of the triangle formatted to two decimal places The perimeter formatted to one decimal place The area formatted to one decimal place and The unformatted area into one dialog box?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Decimal formatting?

    Quote Originally Posted by tinker99 View Post
    how I might get the The three sides of the triangle formatted to two decimal places
    that's what the DecimalFormat object is for. Go to the API and read about the class or search online for tutorials on how to use it.
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Formatting answers to 2 decimal places
    By Cal S. in forum What's Wrong With My Code?
    Replies: 27
    Last Post: September 13th, 2014, 03:17 AM
  2. Java Program - Distance Traveled (Formatting and Decimal Place)
    By javaBoi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 12th, 2014, 09:01 AM
  3. Round Decimal number into 2 decimal
    By mehboob110233 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 2nd, 2014, 08:20 AM
  4. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  5. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM