JAVA取整以及四舍五入

下面来介绍将小数值舍入为整数的几个方法:Math.ceil()、Math.floor()和Math.round()。 这三个方法分别遵循下列舍入规则:
Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;

Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。

下面来看几个例子:

Math.ceil(25.9) //

Math.ceil(25.5) //

Math.ceil(25.1) //

Math.ceil(25.0)
// Math.round(25.9) // Math.round(25.5) // Math.round(25.1) // Math.floor(25.9) // Math.floor(25.5) // Math.floor(25.1) //
import java.math.BigDecimal; //引入这个包

public class Test {
public static void main(String[] args) { double i = 3.856; // 舍掉小数取整
System.out.println("舍掉小数取整:Math.floor(3.856)=" + (int) Math.floor(i)); // 四舍五入取整
System.out.println("四舍五入取整:(3.856)="
+ new BigDecimal(i).setScale(0, BigDecimal.ROUND_HALF_UP)); // 四舍五入保留两位小数
System.out.println("四舍五入取整:(3.856)="
+ new BigDecimal(i).setScale(2, BigDecimal.ROUND_HALF_UP)); // 凑整,取上限
System.out.println("凑整:Math.ceil(3.856)=" + (int) Math.ceil(i)); // 舍掉小数取整
System.out.println("舍掉小数取整:Math.floor(-3.856)=" + (int) Math.floor(-i));
// 四舍五入取整
System.out.println("四舍五入取整:(-3.856)="
+ new BigDecimal(-i).setScale(0, BigDecimal.ROUND_HALF_UP)); // 四舍五入保留两位小数
System.out.println("四舍五入取整:(-3.856)="
+ new BigDecimal(-i).setScale(2, BigDecimal.ROUND_HALF_UP)); // 凑整,取上限
System.out.println("凑整(-3.856)=" + (int) Math.ceil(-i));
} }

JAVA取整以及四舍五入的更多相关文章

  1. 【转】java取整和java四舍五入方法

    java取整和java四舍五入方法 import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetI ...

  2. AVA取整以及四舍五入

    AVA取整以及四舍五入 import java.math.BigDecimal; public class Test { public static void main(String[] args) ...

  3. js 向上取整、向下取整、四舍五入

      js 向上取整.向下取整.四舍五入 CreateTime--2018年4月14日11:31:21 Author:Marydon // 1.只保留整数部分(丢弃小数部分) parseInt(5.12 ...

  4. js取整、四舍五入等数学函数

    js只保留整数,向上取整,四舍五入,向下取整等函数1.丢弃小数部分,保留整数部分parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math. ...

  5. Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

    Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 FLOOR——对给定的数字取整数位SQL> select floor(2345.67) from dua ...

  6. 问题:oracle floor;结果:Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

    Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 (2011-04-06 16:10:35) 转载▼ 标签: 谈 分类: 渐行渐远 FLOOR——对给定的数字取 ...

  7. python 向下取整,向上取整,四舍五入

    # python 向下取整 floor 向上取整ceil 四舍五入 round import math num=3.1415926 # 向上取整 print(math.ceil(num)) # 向下取 ...

  8. js对数字的处理:取整、四舍五入、数字与字符串的转换

    取整.四舍五入 向下取整Math.floor() 向上取整Math.ceil() 四舍五入Math.round()) 保留有效数位n.toFixed() 产生大于等于0小于1的随机数Math.rand ...

  9. java取整和java四舍五入方法 BigDecimal.setScale()方法详解

    import java.math.BigDecimal; public class TestGetInt { public static void main(String[] args) { doub ...

随机推荐

  1. Effective C++_笔记_条款04_确定对象被使用之前已先被初始化

    (整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 读取未初始化的值会导致不确定的行为.在某些平台上,仅仅只是读取为 ...

  2. delphi删除只读文件

    只读文件就是不能删除的文件,用DeleteFile函数对它来说是毫无意义的,要删除只读文件,只有先改变它的属性.如果你要删除一个文件,最好先作两个方面的考虑: (1)判断该文件的属性.可以用上面提到的 ...

  3. shell基础(转)

    shell基础1:文件安全与权限 http://bbs.chinaunix.net/forum/viewtopic.php?t=434579&highlight=wingger 附:Linux ...

  4. 阿斯钢iojeg9uhweu9erhpu9hyw49

    http://www.huihui.cn/share/8424421 http://www.huihui.cn/share/8424375 http://www.huihui.cn/share/842 ...

  5. Android Sip学习(三)Android Voip实现

    Android Sip学习(三)Android Voip实现   Android Sip学习(准备知识)SIP 协议完整的呼叫流程 Android Sip学习(一)Android 2.3 APIs S ...

  6. UVA 839 (13.08.20)

     Not so Mobile  Before being an ubiquous communications gadget, a mobile wasjust a structure made of ...

  7. 七个你无法忽视的Git使用技巧(转)

    与其他技术相比,Git应该拯救了更多开发人员的饭碗.只要你经常使用Git保存自己的工作,你就一直有机会可以将代码退回到之前的状态,因此就可以挽回那些你深夜里迷迷糊糊犯下的错误. 尽管这么说,Git的命 ...

  8. objective-c 中数据类型之中的一个 几何数据类型(CGPoint,CGSize,CGRect)

    // CGPoint 结构体数据原型, 用于声明一个点: /* Points. */ struct CGPoint { CGFloat x; CGFloat y; }; typedef struct ...

  9. [JS练习] 瀑布流照片墙

    记录JS实现瀑布流照片墙效果 首先是前端页面 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  10. Windowbuilder之swt designer安装与使用(转)

    SWT可视化设计,可以使用Google的WindowBuilder. 在Google Code中,搜索WindowBuilder就可以看到路径. 在Eclipse中   Help--->Inst ...