Java 取整

向上取整用Math.ceil(double a)

向下取整用Math.floor(double a) 
举例:

public static void main(String[] args) throws Exception {
        double a = 35;
        double b = 20;
        double c = a / b;
        System.out.println("c===>" + c); // 1.75
        System.out.println("c===>" + Math.ceil(c)); // 2.0
        System.out.println(Math.floor(c)); // 1.0
    }
时间: 2024-08-01 23:40:27

Java 取整的相关文章

java取整和java四舍五入方法

1 import java.math.BigDecimal; 2 import java.text.DecimalFormat; 3 4 public class TestGetInt{ 5 public static void main(String[] args){ 6 double i=2, j=2.1, k=2.5, m=2.9; 7 System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i)); 8 Syst

java取整和java四舍五入方法 转自董俊杰

import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetInt{ public static void main(String[] args){    double i=2, j=2.1, k=2.5, m=2.9;    System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i));    System.out.

java小数取整

java中提供三种小数取整方式 Math.ceil() Math.floor() Math.round() ceil:天花板,向上quzheng Math.ceil(11.5) = 12 Math.ceil(-11.5) = -11 floor:地,向下取整 Math.floor(11.5) = 11 Math.floor(-11.5) = -12 round:4舍5入 Math.round(x + 0.5),再向下取整 当 x = 11.5 时,Math.round(x + 0.5) = Ma

java的四种取整方法

java 中取整操作提供了四种方法:分别是: public static double ceil(double a)//向上取整  public static double floor(double a)//向下取整  public static long round(double a)//四舍五入取整  public static double rint(double a)//最近取整   第一种:ceil是天花板的意思,表示向上取整.   测试: System.out.println(Mat

取整的一些方法总结(java)

Math类中提供了三个与取整有关的方法:ceil.floor.round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil的英文意义是天花板,该方法就表示向上取整,所以,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11:floor的英文意义是地板,该方法就表示向下取整,所以,Math.floor(11.6)的结果为11,Math.floor(-11.6)的结果是-12:最难掌握的是round方法,它表示“四舍五入”,算法为Math.floor(

java 向上、向下取整,四舍五入

public class MathTest { public static void main(String[] args) { // 四舍五入 long round = Math.round(1.499); long round2 = Math.round(1.5); System.out.println(round); System.out.println(round2); // 向上取整 int s = (int)Math.ceil(1.1); System.out.println(s);

java 小时时间就近取整

/** * 时间就近取整 * 08:00 -> 08:00, * 08:20 -> 08:30, * 08:30 -> 08:30, * 08:45 -> 09:00, * 23:56 -> 00:00 * * @param time * @return outTime */public static String getCompleteTime(String time) { String hour = "00";//小时 String minutes =

JavaScript 的 parseInt 取整

http://www.neoease.com/javascript-get-integer-via-parseint/ JavaScript 是弱类型语言, 为了保证数值的有效性, 在处理数值的时候, 我们可以对数值字符串进行强行转换. 如 parseInt 取整和 parseFloat 取浮点数. Java 也有 Integer.parseInt() 方法, 但是 JavaScript 的 parseInt 处理方式与 Java 等强整型语言不太一样, 所以经常有人因为对这个方法的使用不当而获

java%取模(14号)

java取模运算支持类型:字符型(不包括负数),字节型,短整型,整形,长整型,单精度浮点型,双精度浮点型 java奇偶判断 用偶数进行判断 如果用奇数进行判断会出现以下问题: public static void main(String[] args) { int arrs[] = { 2, 1, 0, -1, -2 }; for (int i = 0; i < arrs.length; i++) { System.out.println(arrs[i] + (arrs[i] % 2 == 1