Math round、ceil、floor

不同方法的取舍原则:

ceil:天花板,往大里取

floor:地板,往小里取

round:正数:四舍五入,5是分界点 负数:小数大于5(Math.round(-0.51)输出是-1,-0.5则是0),往小里取,反之,则往大里取

        System.out.println(Math.round(11.6));//12
        System.out.println(Math.round(11.2));//11
        System.out.println(Math.round(11.5));//12
        System.out.println(Math.round(-11.6));//-12
        System.out.println(Math.round(-11.5));//-11
        System.out.println(Math.round(-11.2));//-11
        System.out.println("");
        System.out.println(Math.ceil(11.2));//12
        System.out.println(Math.ceil(11.5));//12
        System.out.println(Math.ceil(11.6));//12
        System.out.println(Math.ceil(-11.2));//-11
        System.out.println(Math.ceil(-11.5));//-11
        System.out.println(Math.ceil(-11.6));//-11
        System.out.println("");
        System.out.println(Math.floor(11.2));//11
        System.out.println(Math.floor(11.5));//11
        System.out.println(Math.floor(11.6));//11
        System.out.println(Math.floor(-11.2));//-12
        System.out.println(Math.floor(-11.5));//-12
        System.out.println(Math.floor(-11.6));//-12

BigDecimal

1.解决小数计算失去精度问题

BigDecimal bd1 = new BigDecimal("0.001");
BigDecimal bd2 = new BigDecimal("0.009");
double add = bd1.add(bd2).doubleValue();
System.out.println(add);

得到正确的0.01

2.小数位数保留

double db = 1.2346013123;
BigDecimal dd = new BigDecimal(db).setScale(2,BigDecimal.ROUND_HALF_UP );//保留两位,小数四舍五入
System.out.println(dd);

原文地址:https://www.cnblogs.com/whwjava/p/9121297.html

时间: 2024-08-06 05:23:23

Math round、ceil、floor的相关文章

Math类中round、ceil和floor方法的功能

Java中的Math工具类用来完成除+.-.*./.%等基本运算以外的复杂运算,位于java.lang包下,Math类的构造器全是私有的(private),因此无法创建Math类的对象,Math类的方法全是类方法,可以直接通过类名来调用它们.下面重点介绍Math类中经常用到的几个方法,也是面试时经常被问到的知识点. 1.round round方法表示四舍五入.round意为“环绕”,其原理是在原数字的基础上先加上0.5再向下取整,它的返回值为int类型,例如,Math.round(11.5)等于

delphi的取整函数round、trunc、ceil和floor

首先引入math单元 uses math; 1.Round(四舍六入五留双) 功能说明:对一个实数进行四舍五入.(按照银行家算法) 例: var i, j: Integer; begin i := Round(1.5); // i等于2 j := Round(2.5); // j等于2 end; 在Delphi中使用Round函数得到的答案有时与我们所预期的会不太一样:采用的是四舍六入五留双.即当舍或入位大于或小于五时按四舍五入来处理 ,而当舍或入位等于五时,就要看前面一位是什么,根据奇进偶不进

JavaScript里Math对象的ceil()、floor()、round()方法的区别

ceil(x) 官方含义:对一个数进行上舍入.理解:ceiling为天花板的意思,意译为向上取整.即取得大于于等于x的最大整数. floor(x) 官方含义:对一个数进行下舍入.理解:floor为地板的意思,意译为向下取整.即取得小于等于x的最大整数. round() 官方含义: 把一个数四舍五入为最接近的整数.理解:传统意义的四舍五入. 例1: var a = 3.5;document.write(Math.floor(a)) ;document.write(Math.ceil(a)) ;do

iOS中的round、ceil、floor函数略解

苹果官方文档位置:usr/include > math.h extern float ceilf(float); extern double ceil(double); extern long double ceill(long double); extern float floorf(float); extern double floor(double); extern long double floorl(longdouble); extern float roundf(float); ex

【转】SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN

--------------------------------------------------------------------------1 trunc(value,precision)按精度(precision)截取某个数字,不进行舍入操作.2 round(value,precision)根据给定的精度(precision)输入数值.3 ceil (value) 产生大于或等于指定值(value)的最小整数.4 floor(value)与 ceil()相反,产生小于或等于指定值(va

php中除法取整的方法(round,ceil,floor)

PHP中遇到需要将除法所得结果取整的情况时,就需要用到以下方法: 1. round:四舍五入 round() 函数对浮点数进行四舍五入. 语法:round(x, prec) 参数 描述 x 可选.规定要舍入的数字. prec 可选.规定小数点后的位数. 说明:返回将 x 根据指定精度 prec (十进制小数点后数字的数目)进行四舍五入的结果.prec 也可以是负数或零(默认值). 提示:PHP 默认不能正确处理类似 "12,300.2" 的字符串. 例: 1 <?php 2 ec

【2017-04-01】JS字符串的操作、时间日期的操作、函数、事件、动画基础

一.字符串的操作 1.转大写: s.toLowerCase(); 2.转大写: s.toUpperCase(); 3.字符串的截取: s.substr(3,4);      -从索引3开始截取,截取4位.索引从0开始. 4.将字符串按指定的字符拆开: s.split(",");             引号内放指定的字符.返回的是一个数组. 5.字符串长度: s.length; 6.字符串中一个字符的索引: s.indexOf("world");      worl

JavaScript(核心、BOM、DOM)

http://www.flyne.org/article/407 JavaScript(核心.BOM.DOM) JavaScript是基于对象和事件驱动的客户端脚本语言.有如下特点: 交互性 安全性(不可以直接访问本地硬盘) 跨平台性(只要是可以解析js的浏览器都可以执行,和平台无关) 1.JavaScript和Java不同! ①所属:NetScape和Sun ②基于对象和面向对象 ③JS可以直接解析执行,Java需先编译在运行 ④JavaScript是一种弱类型的语言,Java是强类型的语言.

Javascript Math ceil()、floor()、round()三个函数的区别

Round是四舍五入的...Ceiling是向上取整..float是向下取整  下面来介绍将小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则:◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数:◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数:◎Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则).