今天看了一道以前的java面试题,又提到了这两个方法,总结一下:
1.Math.round(x):相当于x加1/2,再求其floor,再求整;
2.Math.floor(y):返回所给参数的近似整数值,近似值为向下取整所得的数值,即近似值为小于等于参数的整数。
例子:
public static void main(String[] args) {
//--------------Math.floor()-----------------
//返回所给参数的近似整数值,近似值为向下取整所得的数值,即近似值为小于等于参数的整数
System.out.println(Math.floor(2.1));// 2.0
System.out.println(Math.floor(2.8));// 2.0
System.out.println(Math.floor(-2.1));// -3.0
System.out.println(Math.floor(-2));// -3.0
System.out.println("--------------");
//--------------Math.round()-----------------相当于加1/2,再求其floor,再求整
System.out.println(Math.round(2.5));// 3(四舍五入)
//相当于:
System.out.println((int)Math.floor(2.5+0.5));// 3
System.out.println(Math.round(-2.5));// -3
//相当于:
System.out.println((int)Math.floor(-2.5+0.5));// -3
}
分享到:
相关推荐
首先还是看看[removed] The Definitive Guide, 4th Edition中对三个函数的定义。 Math.ceil(): round a number up ...Math.floor(): round a number down Arguments: Any numeric value or expressi
主要介绍了Java Math.round(),Math.ceil(),Math.floor()的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Math.ceil(Math.random()*10);...Math.floor(Math.random()*10); // 可均衡获取 0 到 9 的随机整数。Math.round(Math.random()*10); // 基本均衡获取 0 到 10 的随机整数,其中获取最小值 0 和最大值 10 的几率少一半。
Math方法使用: Math.ceil, Math.round, Math.floor
Math 类中提供了三个与取整有关的方法:ceil、floor、round,这些方法的作用与它们的英 文名称的含义相对应,例如,ceil 的英文意义是天花板,该方法就表示向上取整, Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的...
Math类取整方法和random随机值方法 Math类 Math类 1 掌握常用数学函数; 2 会使用Math类中的方法解决数学问题。 double ceil( double x) //返回一个大于等于 x的最小整数值 Math.ceil(2.1) 结果为 3.0 Math.ceil(2.0...
<SPAN xss=removed>1.Math... 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数)。 Math.floor(num); 参数num为一个数值,函数结果为num的整数部分(返回小于等于n的最大整数
java加密和解密 function encrypt(str, pwd) { if(pwd == null || pwd.length ) { alert("Please enter a password with which to encrypt the message."); return null; } var prand = ""; for(var i=0; i...
下面来介绍将小数值舍入为整数的几个方法:Math.ceil()、Math.floor()和Math.round()。 这三个方法分别遵循下列舍入规则: ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数; ◎Math.floor()执行...
this.pos = Math.round(Math.random()); this.bubble.style.top = `${this.y}px`; this.bubble.style.left = `${this.x}px`; this.bubble.style.transform = `translateZ(${ this.pos ? "" : "-" }${...
1.java.lang.Math.random() 在所有其他语言中,生成随机数就像是使用Math工具类,如abs, pow, floor, sqrt和其他数学函数。大多数人通过书籍、教程和课程来了解这个类。一个简单的例子:从0.0到1.0之间可以生成一个...
Math.floor(12.9999) = 12 ceil:上进 Math.ceil(12.1) = 13; round: 四舍五入 Math.round(12.5) = 13 Math.round(12.4) = 12 二、小数位数控制 保留到整数: exam = Math.round(exam); 保留一位小数: exam = ...
话不多说,跟着小编一起来看下吧 1、Math.random():返回 0 ~ 1 之间的随机数。...4、Math.floor():返回值:返回小于或等于x,并且与之最接近的整数(如果x是正数,则把小数“舍”;如果x是负数,则把小数“入”。)。
MM = Math.Floor((n - DD) * 100); SS = ((n - DD) * 100 - MM) * 100; n = (DD + MM / 60.0 + SS / 3600.0) * Math.PI / 180.0; return n; } private void Form2_Load(object sender, EventArgs e) { } ...
一、预备知识 Math.ceil(); //向上取整。 Math.floor(); //向下取整。...Math.round(Math.random()); //可均衡获取0到1的随机整数。 Math.floor(Math.random()*10); //可均衡获取0到9的随机整数。 Math.r
Math.round(x):四舍五入 获取指定范围内的随机数 var x=Math.floor(Math.random()*(max-min+1))+min; 以上这篇JavaScript Math 对象常用方法总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家...
Math.floor(x) 对数进行下舍入 Math.round(x) 把数四舍五入为最接近的整数 Math.max(x,y) 返回 x 和 y 中的最高值 Math.min(x,y) 返回 x 和 y 中的最低值 Math.pow(x,y) 返回 x 的 y 次幂 Math.s
返回数字的下舍整数,如math.floor(4.9)返回 4 log(x) 如math.log(math.e)返回1.0,math.log(100,10)返回2.0 log10(x) 返回以10为基数的x的对数,如math.log10(100)返回 2.0 max(x1, x2,...) 返回给定参数的最大值,...