栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Java/C语言数据取整

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java/C语言数据取整

1. Java取整 1.强制类型转换
public class Main {
    public static void main(String[] args){
        System.out.println("正数:(9.12) = " + (int)9.12);
        System.out.println("负数:(-9.12) = " + (int)-9.12);
    }
}

结果:向0取整, 忽略小数点后面的部分

(9.12) = 9
(-9.12) = -9
2. Java三个取整函数 1. Math.ceil(double num);//ceil为天花板,意为向上取整 2. Math.floor(double num);//floor为地板,意为向下取整
public class Main {
    public static void main(String[] args){
        System.out.println("正数:Math.ceil(9.12) = " + Math.ceil(9.12));
        System.out.println("负数:Math.ceil(-9.12) = " + Math.ceil(-9.12));
        System.out.println("---------------------------------");
        System.out.println("正数:Math.floor(9.12) = " + Math.floor(9.12));
        System.out.println("负数:Math.floor(-9.12) = " + Math.floor(-9.12));

    }

}

结果:

Math.ceil 向上取整
Math.floor向下取整
Math.ceil(9.12) = 10.0
Math.ceil(-9.12) = -9.0
---------------------------------
Math.floor(9.12) = 9.0
Math.floor(-9.12) = -10.0
3. Math.round(double num);//Math.round就是对num+0.5之后向下取整
public class Main {
    public static void main(String[] args){
        System.out.println("小数点后第一位>5");
        System.out.println("正数:Math.round(10.68) = " + Math.round(10.68));
        System.out.println("负数:Math.round(-10.68) = " + Math.round(-10.68));
        System.out.println();

        System.out.println("小数点后第一位=5");
        System.out.println("正数:Math.round(10.5) = " + Math.round(10.5));
        System.out.println("负数:Math.round(-10.5) = " + Math.round(-10.5));
        System.out.println();
    
        System.out.println("小数点后第一位<5");
        System.out.println("正数:Math.round(10.46) = " + Math.round(10.46));
        System.out.println("负数:Math.round(-10.46) = " + Math.round(-10.46));
        System.out.println();
        
    }

}

即为: [ Math.floor(x+0.5) ] (向下取整)

即将原来的数字加上0.5后再向下取整


c语言取整 1. 使用int强制转换(同Java)
使用int强制转换的结果就是去掉小数部分。

 int a;
 a=int(1.2); //a=1
 a=int(-1.2); //a=-1
2. 两个取整函数 1. 使用floor函数向下取整(同JavaMath.floor)
使用floor函数的结果是向下取整,即得到比该数小的最大整数,需要包含头文件math.h
int a;
a=floor(1.2); //a=1
a=floor(-1.2); //a=-2
2. 使用ceil函数向上取整(同JavaMath.ceil)
使用ceil函数的结果是向上取整,即得到比该数大的最小整数,需要包含头文件math.h
int a;
a=ceil(1.2); //a=2
a=ceil(-1.2); //a=-1
3. 若需要向最邻近整数取整(同JavaMath.round)
float b=1.2;
int a;
b=b>=0 ? b+0.5:b-0.5;
a=int(b);

逻辑右移和算术右移 C语言
  • 无符号数:逻辑右移
  • 有符号数:没有明确定义,但几乎所有编译器都对有符号数使用算术右移
Java
  • Java只支持有符号数
  • 支持选择右移模式:
    • x>>k 算术右移
    • x>>>k 逻辑右移

Java向0取整方法:
java默认向0取整

    int a1 = 13;
    int a2 = 14;
    int a3 = 15;
    int a4 = 16;
    int a5 = 17;
    int a6 = 20;
    int b = 10;

    System.out.println(a1 + " " + a1 / b);
    System.out.println(a2 + " " + a2 / b);
    System.out.println(a3 + " " + a3 / b);
    System.out.println(a4 + " " + a4 / b);
    System.out.println(a5 + " " + a5 / b);


结果:

13 1
14 1
15 1
16 1
17 1
13 -1
14 -1
15 -1
16 -1
17 -1
20 -2

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号