Java precision

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paradox6996
    New Member
    • Jul 2007
    • 21

    Java precision

    [code=java]import java.util.Scann er;

    public class Program01C
    {

    public static void main(String[] args)
    {
    double purchase, totalTax, salesTax, countyTax, total;
    salesTax = 0.04;
    countyTax = 0.02;


    Scanner keyboard = new Scanner(System. in);
    System.out.prin t("Enter the amount of purchase: ");
    purchase = keyboard.nextIn t();

    totalTax = (salesTax + countyTax);
    total = (((totalTax) * purchase) + purchase);
    System.out.prin tln("Sales Tax:" + salesTax + "\nCounty Tax:" + countyTax + "\nTotal Sales Tax:" + totalTax + "\nTotal Purchase:" + purchase + "\nTotal Amount of Purchase:" + total);

    I want to add precision to total variable at the end of the program by 2, but forgot how to do it:

    "\nTotal Amount of Purchase:" + total);

    Thanx, Paradox(>")>

    Question num# 2:

    import java.util.Scann er;

    public class Program01B
    {

    public static void main(String[] args)
    {
    double testScore01, testScore02, testScore03, totalAverage, totalAveragemod ;

    Scanner keyboard = new Scanner(System. in);
    System.out.prin t("Enter Test Score 1:");
    testScore01 = keyboard.nextIn t();
    System.out.prin t("Enter Test Score 2:");
    testScore02 = keyboard.nextIn t();
    System.out.prin t("Enter Test Score 3:");
    testScore03 = keyboard.nextIn t();

    //Division of 3 testscores plus remainder(Modul us)
    totalAverage = ((testScore01 + testScore02 + testScore03) / 3);
    totalAveragemod = ((testScore01 + testScore02 + testScore03) % 3);

    System.out.prin tln("Test Score 1:" + testScore01 + "\nTest Score 2:" + testScore02 + "\nTest Score 3:" + testScore03 + "\nTotal Average of Test Scores:" + totalAverage + "." + totalAveragemod );
    }
    }
    [/code]
    The modulus outputs as so:
    Enter Test Score 1:80
    Enter Test Score 2:90
    Enter Test Score 3:80
    Test Score 1:80.0
    Test Score 2:90.0
    Test Score 3:80.0
    Total Average of Test Scores:83.33333 333333333.1.0

    How can I make so it outputs in decimal form(00.00) or if it needs to have precision added to it then ok, but it has 3 decimal places so IDK!

    Thanx, Paraodx(>")>
    Last edited by pbmods; Feb 6 '09, 03:55 AM. Reason: Added CODE tags.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Have a look at System.out.prin tf

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      Java has similar formatted output facilities to printf() in C, see

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by horace1
        Java has similar formatted output facilities to printf() in C, see
        http://java.sun.com/developer/techni...mming/sprintf/
        That article is soooo old; better read what r035198x wrote.

        kind regards,

        Jos

        Comment

        • paradox6996
          New Member
          • Jul 2007
          • 21

          #5
          I changed the code to this:

          Code:
          //Robert Burns
          //Feb. 5, 2009
          //paradox6996@gmail.com
          //Program01B-Calculates total average of test scores
          
          import java.util.Scanner;
          import static java.lang.Math.*;
          
          public class Program01B {
           
           public static void main(String[] args) {
          
            double testScore01, testScore02, testScore03, totalAverage, totalAveragemod;
          
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter Test Score 1:");
            testScore01 = keyboard.nextDouble();
            System.out.print("Enter Test Score 2:");
            testScore02 = keyboard.nextDouble();
            System.out.print("Enter Test Score 3:");
            testScore03 = keyboard.nextDouble();
          
          //Division of 3 test scores plus remainder(Modulus)
            totalAverage = ((testScore01 + testScore02 + testScore03) / 3);
            totalAveragemod = ((testScore01 + testScore02 + testScore03) % 3);
          
            System.out.printf("Test Score 1:" + testScore01 + "\nTest Score 2:" + testScore02 + "\nTest Score 3:" + testScore03 + "\nTotal Average of Test Scores:%.2f", totalAverage, Math.ceil(totalAverage));
           }
          }
          This is the output:

          Enter Test Score 1:85
          Enter Test Score 2:90.5
          Enter Test Score 3:88.2
          Test Score 1:85.0
          Test Score 2:90.5
          Test Score 3:88.2
          Total Average of Test Scores:87.90

          Thanx for the help, Paradox(>")>

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            You don't need that variable totalAveragemod at all.

            kind regards,

            Jos

            Comment

            • paradox6996
              New Member
              • Jul 2007
              • 21

              #7
              Originally posted by JosAH
              You don't need that variable totalAveragemod at all.

              kind regards,

              Jos
              I know I took it out to get this:

              Code:
              //Robert Burns
              //Feb. 5, 2009
              //paradox6996@gmail.com
              //Program01B-Calculates total average of test scores
              
              import java.util.Scanner;
              
              //Math class so I can truncate doubles to get nice looking decimals 
              import static java.lang.Math.*;
              
              public class Program01B {
               
               public static void main(String[] args) {
              
                double testScore01, testScore02, testScore03, totalAverage;
              
                Scanner keyboard = new Scanner(System.in);
                System.out.print("Enter Test Score 1:");
                testScore01 = keyboard.nextDouble();
                System.out.print("Enter Test Score 2:");
                testScore02 = keyboard.nextDouble();
                System.out.print("Enter Test Score 3:");
                testScore03 = keyboard.nextDouble();
              
              //Division of 3 test scores
                totalAverage = ((testScore01 + testScore02 + testScore03) / 3);
              
                System.out.printf("Test Score 1: " + testScore01 + "\nTest Score 2: " + testScore02 + "\nTest Score 3: " + testScore03 + "\nTotal Average of Test Scores: %.2f", totalAverage, Math.ceil(totalAverage));

              Comment

              Working...