博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java四舍五入
阅读量:4286 次
发布时间:2019-05-27

本文共 1393 字,大约阅读时间需要 4 分钟。

Java有四舍五入函数--Math.round,通过一个例子看看他的用法:

  1. package math;  
  2.   
  3. public class MathRoundTest {  
  4.     /** 
  5.      * Math类中提供了三个与取整有关的方法:ceil,floor,round, 
  6.      * 这些方法的作用于它们的英文名称的含义相对应,例如:ceil的英文意义是天花板,该方法就表示向上取整, 
  7.      * Math.ceil(11.3)的结果为12,Math.ceil(-11.6)的结果为-11;floor的英文是地板, 
  8.      * 该方法就表示向下取整,Math.floor(11.6)的结果是11,Math.floor(-11.4)的结果-12; 
  9.      * 最难掌握的是round方法,他表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整, 
  10.      * 所以,Math.round(11.5)的结果是12,Math.round(-11.5)的结果为-11.Math.round( )符合这样的规律: 
  11.      * 小数点后大于5全部加,等于5正数加,小于5全不加。 
  12.      */  
  13.     public static void main(String[] args) {     
  14.         System.out.println("小数点后第一位=5");     
  15.         System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));     
  16.         System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));     
  17.         System.out.println();     
  18.     
  19.         System.out.println("小数点后第一位<5");     
  20.         System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));     
  21.         System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));     
  22.         System.out.println();     
  23.     
  24.         System.out.println("小数点后第一位>5");     
  25.         System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));     
  26.         System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));  
  27.         /** 
  28.          * 运行结果: 
  29.             1、小数点后第一位=5 
  30.             2、正数:Math.round(11.5)=12 
  31.             3、负数:Math.round(-11.5)=-11 
  32.             4、 
  33.             5、小数点后第一位<5 
  34.             6、正数:Math.round(11.46)=11 
  35.             7、负数:Math.round(-11.46)=-11 
  36.             8、 
  37.             9、小数点后第一位>5 
  38.             10、正数:Math.round(11.68)=12 
  39.             11、负数:Math.round(-11.68)=-12 
  40.          */  
  41.         /** 
  42.          * 1、参数的小数点后第一位<5,运算结果为参数整数部分。 
  43.            2、参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。 
  44.            3、参数的小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。 
  45.          */  
  46.     }     
  47.   

转载地址:http://itxgi.baihongyu.com/

你可能感兴趣的文章
iOS之cocoaPods发布产品详解
查看>>
java之国际化
查看>>
java 之注解
查看>>
java之struts(二)国际化、拦截器
查看>>
iOS之真机和模拟器的CPU架构器架构\Xcode中和symbols有关的几个设置
查看>>
java之maven的使用/eclipse中maven项目部署到tomcat的几种方法
查看>>
iOS之常用分类frame、button、
查看>>
微信小程序常用快捷键
查看>>
小程序中相关控件的使用、样式的使用、flex布局
查看>>
开篇词
查看>>
论文笔记:Event Detection with Trigger-Aware Lattice Neural Network
查看>>
《Recurrent Chunking Mechanisms for Long-Text Machine Reading Comprehension》--论文分享
查看>>
论文笔记丨Multi-Level Matching and Aggregation Network for Few-Shot Relation Classification
查看>>
论文笔记 | All NLP Tasks Are Generation Tasks: A General Pretraining Framework
查看>>
论文笔记 | Towards Interpreting BERT for Reading Comprehension Based QA
查看>>
论文笔记 | Transformer-XL:Attentive Language Models Beyond a Fixed-Length Context
查看>>
论文笔记 | An End-to-End Deep Framework for Answer Triggering with a Novel Group-Level Objective
查看>>
论文笔记:Exploiting WordNet Synset and Hypernym Representations for Answer Selection
查看>>
论文笔记|Overcoming the challenge for text classification in the open world
查看>>
论文笔记 | FLAT: Chinese NER Using Flat-Lattice Transformer
查看>>