Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program: Function floor ( found in the math library ) may be used to round a num

ID: 3730491 • Letter: P

Question

Program:

Function floor (found in the math library) may be used to round a number to a specific decimal place. The statement:
y= floor (x * 10 + .5) / 10;
rounds x to the tenths position (the first position to the right of the decimal place. The statement
y = floor(x*100 +.5) / 100;
rounds x to the hundredths position (2nd to the right of decimal place).

Write a program that defines four functions to round a number x in a number of ways:
A. float roundToInteger( float number )
B. float roundToTenths(float number )
C. float roundToHundredths( float number )
D. float roundToThousandths( float number )

In your program, ask the user to enter a floating point number. Then, your program should print the original value, the number rounded nearest int, nearest tenth, nearest hundredth and nearest thousandth. Use the math library function “floor” to calculate.

So, your program will have 5 functions that you created.

main -- where you will ask for a number from the user, and get it.

roundToInteger( number )
roundToTenths( number )
roundToHundredths( number )
roundToThousandths( number )

Explanation / Answer

import java.util.Scanner;
import java.lang.*;


class Main {
public static void main(String[] args) {
float f=0;
Scanner input = new Scanner(System.in);
System.out.println("Please enter Floating point Number : ");
f = input.nextFloat();
System.out.println("Original Value = "+ f);
roundToInteger(f);
roundToTenths(f);
roundToHundredths(f);
roundToThousandths(f);
}
  
public static void roundToInteger(float f){
System.out.println("Math.floor(" + f + ") = " +(int) Math.floor(f));
}
  
public static void roundToTenths(float f){
System.out.println("Math.floor(" + f + ") = " +
Math.floor(f*10 +.5)/10);
}
  
public static void roundToHundredths(float f){
System.out.println("Math.floor(" + f + ") = " +
Math.floor(f*100 +.5)/100);
}
  
public static void roundToThousandths(float f){
System.out.println("Math.floor(" + f + ") = " +
Math.floor(f*1000 +.5)/1000);
}
}