/* Name: Joseph Schuman Date: 2/6/2015 File: Chapter 6 Assignment Descripetion: Takes a decimal number and rounds it to the nearest integer value. Methods are used to separate the rounding method from the main method. */ import java.util.Scanner; public class CPT236DecimalRoundMethod { public static void main(String[] args) { //Main method //Create the Scanner object. Scanner input = new Scanner(System.in); //Prompt the user to enter a decimal number. System.out.print("Enter a decimal number: "); double dec1 = input.nextDouble(); System.out.print("Enter a number to round by decimal places: "); int decRnd = input.nextInt(); System.out.println("The value "+ dec1 + " is rounded to " + decimalRound(dec1, decRnd)); } //Return the rounded decimal. public static double decimalRound(double dec1, int decRnd) { //Covert decimal to integer and round to 'decRnd' decimal places. dec1 += 5.0/Math.pow(10, decRnd + 1); dec1 *= Math.pow(10, decRnd); long dec2 = (long) dec1; double finalDec = (double)dec2/(Math.pow(10, decRnd)); return finalDec; } }