Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

java取整

作者:身后错过痛过漫长情节ゝ   发布日期:2025-11-20   浏览:47

// Java取整示例代码

public class RoundExample {
    public static void main(String[] args) {
        double num = 12.75;

        // 使用 Math.floor() 向下取整
        int floorValue = (int) Math.floor(num);
        System.out.println("向下取整: " + floorValue);  // 输出 12

        // 使用 Math.ceil() 向上取整
        int ceilValue = (int) Math.ceil(num);
        System.out.println("向上取整: " + ceilValue);  // 输出 13

        // 使用 Math.round() 四舍五入取整
        int roundValue = (int) Math.round(num);
        System.out.println("四舍五入取整: " + roundValue);  // 输出 13
    }
}

解释说明:

  • Math.floor():向下取整,返回小于或等于给定数的最大整数。
  • Math.ceil():向上取整,返回大于或等于给定数的最小整数。
  • Math.round():四舍五入取整,返回最接近的整数。

上一篇:java中多个三目运算符怎么写?

下一篇:java assert用法

大家都在看

java连接数据库的代码

java djl

ubuntu 卸载java

java读取excel中的图片

java新建

java sort用法

java collections.sort

java file类的方法

java发送qq邮件

java 判断

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站