471,616 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Java precision

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Program01C
  4. {
  5.  
  6.  public static void main(String[] args)
  7.  {
  8.   double purchase, totalTax, salesTax, countyTax, total;
  9.   salesTax = 0.04;
  10.   countyTax = 0.02;
  11.  
  12.  
  13.   Scanner keyboard = new Scanner(System.in);
  14.   System.out.print("Enter the amount of purchase: ");
  15.   purchase = keyboard.nextInt();
  16.  
  17.   totalTax = (salesTax + countyTax);
  18.   total = (((totalTax) * purchase) + purchase);
  19.   System.out.println("Sales Tax:" + salesTax + "\nCounty Tax:" + countyTax + "\nTotal Sales Tax:" + totalTax + "\nTotal Purchase:" + purchase + "\nTotal Amount of Purchase:" + total);
  20.  
  21. I want to add precision to total variable at the end of the program by 2, but forgot how to do it:
  22.  
  23. "\nTotal Amount of Purchase:" + total);
  24.  
  25. Thanx, Paradox(>")>
  26.  
  27. Question num# 2:
  28.  
  29. import java.util.Scanner;
  30.  
  31. public class Program01B
  32. {
  33.  
  34.  public static void main(String[] args)
  35.  {
  36.   double testScore01, testScore02, testScore03, totalAverage, totalAveragemod;
  37.  
  38.   Scanner keyboard = new Scanner(System.in);
  39.   System.out.print("Enter Test Score 1:");
  40.   testScore01 = keyboard.nextInt();
  41.   System.out.print("Enter Test Score 2:");
  42.   testScore02 = keyboard.nextInt();
  43.   System.out.print("Enter Test Score 3:");
  44.   testScore03 = keyboard.nextInt();
  45.  
  46. //Division of 3 testscores plus remainder(Modulus)
  47.   totalAverage = ((testScore01 + testScore02 + testScore03) / 3);
  48.   totalAveragemod = ((testScore01 + testScore02 + testScore03) % 3);
  49.  
  50.   System.out.println("Test Score 1:" + testScore01 + "\nTest Score 2:" + testScore02 + "\nTest Score 3:" + testScore03 + "\nTotal Average of Test Scores:" + totalAverage + "." + totalAveragemod);
  51.  }
  52. }  
  53.  
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.33333333333333.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(>")>
Feb 6 '09 #1
6 3048
r035198x
13,262 8TB
Have a look at System.out.printf
Feb 6 '09 #2
horace1
1,510 Expert 1GB
Java has similar formatted output facilities to printf() in C, see
http://java.sun.com/developer/techni...mming/sprintf/
Feb 6 '09 #3
JosAH
11,448 Expert 8TB
@horace1
That article is soooo old; better read what r035198x wrote.

kind regards,

Jos
Feb 6 '09 #4
I changed the code to this:

Expand|Select|Wrap|Line Numbers
  1. //Robert Burns
  2. //Feb. 5, 2009
  3. //paradox6996@gmail.com
  4. //Program01B-Calculates total average of test scores
  5.  
  6. import java.util.Scanner;
  7. import static java.lang.Math.*;
  8.  
  9. public class Program01B {
  10.  
  11.  public static void main(String[] args) {
  12.  
  13.   double testScore01, testScore02, testScore03, totalAverage, totalAveragemod;
  14.  
  15.   Scanner keyboard = new Scanner(System.in);
  16.   System.out.print("Enter Test Score 1:");
  17.   testScore01 = keyboard.nextDouble();
  18.   System.out.print("Enter Test Score 2:");
  19.   testScore02 = keyboard.nextDouble();
  20.   System.out.print("Enter Test Score 3:");
  21.   testScore03 = keyboard.nextDouble();
  22.  
  23. //Division of 3 test scores plus remainder(Modulus)
  24.   totalAverage = ((testScore01 + testScore02 + testScore03) / 3);
  25.   totalAveragemod = ((testScore01 + testScore02 + testScore03) % 3);
  26.  
  27.   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));
  28.  }
  29. }  
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(>")>
Feb 6 '09 #5
JosAH
11,448 Expert 8TB
You don't need that variable totalAveragemod at all.

kind regards,

Jos
Feb 7 '09 #6
@JosAH
I know I took it out to get this:

Expand|Select|Wrap|Line Numbers
  1. //Robert Burns
  2. //Feb. 5, 2009
  3. //paradox6996@gmail.com
  4. //Program01B-Calculates total average of test scores
  5.  
  6. import java.util.Scanner;
  7.  
  8. //Math class so I can truncate doubles to get nice looking decimals 
  9. import static java.lang.Math.*;
  10.  
  11. public class Program01B {
  12.  
  13.  public static void main(String[] args) {
  14.  
  15.   double testScore01, testScore02, testScore03, totalAverage;
  16.  
  17.   Scanner keyboard = new Scanner(System.in);
  18.   System.out.print("Enter Test Score 1:");
  19.   testScore01 = keyboard.nextDouble();
  20.   System.out.print("Enter Test Score 2:");
  21.   testScore02 = keyboard.nextDouble();
  22.   System.out.print("Enter Test Score 3:");
  23.   testScore03 = keyboard.nextDouble();
  24.  
  25. //Division of 3 test scores
  26.   totalAverage = ((testScore01 + testScore02 + testScore03) / 3);
  27.  
  28.   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));
Feb 7 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

73 posts views Thread by RobertMaas | last post: by
33 posts views Thread by patrick_woflian | last post: by
2 posts views Thread by chad_walters | last post: by
318 posts views Thread by King Raz | last post: by
49 posts views Thread by aarklon | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.