473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 3121
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

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

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
1
by: jweaver | last post by:
I'm having a problem getting back appropriate metadata for stored procedures from MS SQL Server 2000 and 2005. I've created a simple stored procedure that has an output param that is a cursor. ...
2
by: chad_walters | last post by:
Does JavaScript have a mechanism to provide the equivalent of Java's Double.doubleToLongBits() and the reverse operation Double.longBitsToDouble? ...
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
49
by: aarklon | last post by:
Hi all, See:- http://www.cs.princeton.edu/introcs/faq/c2java.html for C vs Java in number crunching http://husnusensoy.blogspot.com/2006/06/c-vs-java-in-number-crunching.html
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.