Java

Math.floor

Math

import java.lang.Math;
public class Program {
public static void main(String[] args) {
*// Compute ceilings of these values.*
double value = Math.ceil(1.1);
double value2 = Math.ceil(-0.9);
System.out.println(value);
System.out.println(value2);
}
}2.0
-0.0

public class Program {
public static void main(String[] args) {
*// Analyze these numbers.*
double[] values = { 1.0, 1.1, 1.5, 1.9, 2.0 };
for (double value : values) {
*// Compute the floor and the ceil for the number.*
double floor = Math.floor(value);
double ceil = Math.ceil(value);
*// See if the floor equals the ceil.*
boolean equal = floor == ceil;
*// Print the values.*
System.out.println(value + *", Floor = "* + floor +
*", Ceil = "* + ceil +
*", Equal = "* + equal);
}
}
}1.0, Floor = 1.0, Ceil = 1.0, Equal = true
1.1, Floor = 1.0, Ceil = 2.0, Equal = false
1.5, Floor = 1.0, Ceil = 2.0, Equal = false
1.9, Floor = 1.0, Ceil = 2.0, Equal = false
2.0, Floor = 2.0, Ceil = 2.0, Equal = true

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Jan 24, 2024 (edit).