数据的截尾和舍人

来源:互联网 发布:linux中rm命令详解 编辑:程序博客网 时间:2024/06/11 09:46

截尾的情况:

代码

public static void main(String[] args) {    double above = 0.7, below = 0.4;    float fabove = 0.7f, fbelow = 0.4f;    print("(int)above: " + (int)above);    print("(int)below: " + (int)below);    print("(int)fabove: " + (int)fabove);    print("(int)fbelow: " + (int)fbelow);  }

结果

(int)above: 0(int)below: 0(int)fabove: 0(int)fbelow: 0 


舍人的情况(需要用到Math.round()方法):

代码

public static void main(String[] args) {      double above = 0.7, below = 0.4;      float fabove = 0.7f, fbelow = 0.4f;      print("Math.round(above): " + Math.round(above));      print("Math.round(below): " + Math.round(below));      print("Math.round(fabove): " + Math.round(fabove));      print("Math.round(fbelow): " + Math.round(fbelow));    }

结果

Math.round(above): 1Math.round(below): 0Math.round(fabove): 1Math.round(fbelow): 0


 

原创粉丝点击