- By using java.lang.math.round in java we will get crossest numbers.
- If we pass double number with some decimal points it will returns closest integer or long number.
- If we pass float number with some decimal points it will return closest integer or long number.
- Lets see an example program on how to use math.random() method.
- Math.random() method will returns closest int or long numbers.
Java program to round double number using math.random() method in java
- package com.mathrandommethod;
- public class MathRandom {
- /**
- * Math.random() in java
- * @author www.instanceofjava.com
- */
- public static void main(String[] args) {
- float float1 = 23.34f;
- float float2 = 23.623f;
- double double1 = 234.433;
- double double2 = 234.654;
- System.out.println(Math.round(float1));
- System.out.println(Math.round(float2));
- System.out.println(Math.round(double1));
- System.out.println(Math.round(double2));
- }
- }
- 23
- 24
- 234
- 235
No comments