zoukankan      html  css  js  c++  java
  • 数据的截尾与舍入

    在将float或double转型为整型值时,总是对该数字执行截尾,如下例:

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

    输出:

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

    如果想要得到舍入的结果,就需要使用java.lang.Math中的round()方法:

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

    输出:

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

    由于round()是java.lang的一部分,因此在使用它事不需要额外的导入。

    推荐一个自己业余时间开发的网盘搜索引擎,360盘搜www.360panso.com

  • 相关阅读:
    sessionStorage用于分页,瀑布流和存储用户数据等
    js瀑布流
    sql 日结
    js 去除html标签
    c# 去除文本的html标签
    jQuery 数据滚动(上下)
    jQuery 图片随滚动条滚动加载
    sql 指定范围 获取随机数
    js 时间格式化
    js自写字符串 append 方法
  • 原文地址:https://www.cnblogs.com/eczhou/p/2284407.html
Copyright © 2011-2022 走看看