Rounding in Java – Math.Round, Math.Floor, Math.Ceil
We have decimal values in java, but sometimes there is a need to round them. It is important to remember which function should be used according to the requirements. So In this blog, I will be sharing some ways to round off decimal values in java. Checking whether java rounds the value up or down by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.*; public class checking { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the value 1: "); int val1=sc.nextInt(); System.out.print("Enter the value 2: "); int val2=sc.nextInt(); System.out.println("The result of the division of value 1 with value 2 is: "+ (val1/val2)); } } |
Output:
1 2 3 |
Enter the value 1: 15 Enter the value 2: 4 The result of the division of value 1 with value 2 is: 3 |
Explanation: We are taking two values val1 and val2 from the user as inputs. Then we are dividing both of
Read more