2 Replies - 104944 Views - Last Post: 30 April 2008 - 05:12 PM Rate Topic: -----

#1 Java Boy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-September 03

How To Round Double To One Decimal Place

Posted 20 September 2003 - 11:13 PM

hello! I am new to Java, so I hope some of you experts here can help me out.
here is my source code
//********************************************************************************************
// grade.java  Author: Wei-Yi(Eric) Chiang
//
//a Java program that prompts a CPSC 111 student for their grades in the course and their 
//projected final exam grade and then computes their overall grade for the course.
//********************************************************************************************

import java.io.*;
import java.text.NumberFormat;

public class grade
{
	public static void main (String[] args) throws IOException
	{
  BufferedReader in =
    new BufferedReader (new InputStreamReader (System.in));

  String name;
  double labGrade,assignGrade,midGrade,finalGrade,totalGrade;
    
  
  //prompts user to enter grades
  System.out.print ("enter your name: ");
  name = in.readLine();
  
  System.out.println ("All grades must be entered as percentage grades."); 
  System.out.println ();
	
  System.out.print ("enter average grade on labs: ");
  labGrade = Double.parseDouble (in.readLine());
  
  
  System.out.print ("enter average grade on assignments: ");
  assignGrade = Double.parseDouble (in.readLine());
  

  System.out.print ("enter projected grade on midterms: ");
  midGrade = Double.parseDouble (in.readLine());
	
	
  System.out.print ("enter average grade on final exam: ");
  finalGrade = Double.parseDouble (in.readLine());
  
  
  
  totalGrade = (labGrade*0.05)+(assignGrade*0.15)+(midGrade*0.30)+(finalGrade*0.50);
  
  
  System.out.println (name + ", your overall grade for CPSC 111 is " + totalGrade + " %!");
   }
} 
  




I would like to print out my result of totalGrade to ONE decimal place one. Can anyone tell me which methods to use? Thanx!

Is This A Good Question/Topic? 0
  • +

Replies To: How To Round Double To One Decimal Place

#2 megamu  Icon User is offline

  • Information Designer
  • member icon

Reputation: 0
  • View blog
  • Posts: 1,829
  • Joined: 13-May 01

Re: How To Round Double To One Decimal Place

Posted 23 September 2003 - 12:32 PM

double rounded = int(number*10)/10;
Was This Post Helpful? 0
  • +
  • -

#14 C:/Syntax  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 35
  • Joined: 12-April 08

Re: How To Round Double To One Decimal Place

Posted 30 April 2008 - 05:12 PM

import java.io.*;
import java.text.*; //need for DecimalFormat class
public class PayRoll12{
  public static void main (String [] args){
	DecimalFormat twoDigit = new DecimalFormat("#,##0.000");//formats to 3 decimal places
	DecimalFormat twoDigit = new DecimalFormat("#,##0.00");//formats to 2 decimal places
	DecimalFormat oneDigit = new DecimalFormat("#,##0.0");//format to 1 decimal place
	/**
	 *can format to any decimal place, just by editting the "zeros"("#,##0.000"), 
	 *and changing to a meaningful varible name(threeDigit)
	 */ 

	  System.out.println(twoDigit.format(anyVariable));
	  System.out.println(oneDigit.format(anyVariable));

  }
}


I like this much better then the upper post
Was This Post Helpful? 1

Page 1 of 1