1.先说下怎么理解

round()方法可以这样理解:

将括号内的数+0.5之后,向下取值,

比如:round(3.4)就是3.4+0.5=3.9,向下取值是3,所以round(3.4)=3;

round(-10.5)就是-10.5+0.5=-10,向下取值就是-10,所以round(-10.5)=-10

所以,Math.round(11.5)=12;

现在再来看,Math.round(11.5),Math.round(-11.5)你应该知道等于多少了吧,掌握了方法就好解决问题了。

这个题面试了很多家就一家遇到,所以就来和大家分享下。

扩展:常用的三个

Math.ceil求最小的整数,但不小于本身.

ceil的英文意义是天花板,该方法就表示向上取整,

例子:

所以,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;

  1. /**
  2. * @see  求最小的整数,但不小于本身
  3. * @param double
  4. * @return double
  5. */
  6. System.out.println(Math.ceil(-1.1));
  7. System.out.println(Math.ceil(-1.9));
  8. System.out.println(Math.ceil(1.1));
  9. System.out.println(Math.ceil(1.9));

输出结果:

  1. -1.0
  2. -1.0
  3. 2.0
  4. 2.0

Math.floor求最大的整数,但不大于本身.

floor的英文意义是地板,该方法就表示向下取整,

例子:

floor的英文意义是地板,该方法就表示向下取整,

所以,Math.floor(11.6)的结果为11,Math.floor(-11.6)的结果是-12;

  1. /**
  2. * @see 求最大的整数,但不大于本身
  3. * @param double
  4. * @return double
  5. */
  6. System.out.println(Math.floor(-1.1));
  7. System.out.println(Math.floor(-1.9));
  8. System.out.println(Math.floor(1.1));
  9. System.out.println(Math.floor(1.9));

输出结果:

  1. -2.0
  2. -2.0
  3. 1.0
  4. 1.0

Math.round求本身的四舍五入.

  1. /**
  2. * @see 本身的四舍五入
  3. * @param double
  4. * @return long
  5. */
  6. System.out.println(Math.round(-1.1));
  7. System.out.println(Math.round(-1.9));
  8. System.out.println(Math.round(1.1));
  9. System.out.println(Math.round(1.9));

输出结果:

  1. -1
  2. -2
  3. 1
  4. 2

Math.abs求本身的绝对值.

  1. /**
  2. * @see 本身的绝对值
  3. * @param double|float|int|long
  4. * @return double|float|int|long
  5. */
  6. System.out.println(Math.abs(1.1));
  7. System.out.println(Math.abs(1.9));
  8. System.out.println(Math.abs(-1.1));
  9. System.out.println(Math.abs(-1.9));

输出结果:

  1. 1.1
  2. 1.9
  3. 1.1
  4. 1.9

Math.max与Math.min,比较两个数的最大值,最小值

  1. /**
  2. * @see 比较两个数的最大值,最小值
  3. * @param double|float|int|long
  4. * @return double|float|int|long
  5. */
  6. System.out.println(Math.max(1.0, 2.0));
  7. System.out.println(Math.min(-1.0, -2.0));

输出结果:

  1. 2.0
  2. -2.0

返回一个与第二个参数相同的标志(正负号)的值

  1. /**
  2. * @see 返回一个与第二个参数相同的标志(正负号)的值
  3. * @param double|float
  4. * @return double|float
  5. */
  6. System.out.println(Math.copySign(-1.9, 2.9));
  7. System.out.println(Math.copySign(1.9, -2.9));
  8. System.out.println(Math.copySign(0.0, 2.9));
  9. System.out.println(Math.copySign(0.0, -2.9));

输出结果:

  1. 1.9
  2. -1.9
  3. 0.0
  4. -0.0

Math

类的常用方法

封装了一些基本运算方法,包括进行三角运算的正弦、余弦、正切、余切相关的方法:

例如,求正弦的

sin

,求余弦的

cos

等,如果使用的话可以参考

JDK

。

下面的方法可能是我们经常要使用的:

(

1

)求最大值,可以用于求

int

类型,

long

类型,

float

类型,

double

类型的最大值,

下面仅仅下求整数最大值的方法的定义:

public static int max(int a,int b);

(

2

)求最小值,和求最大值基本相同。

public static int min(int a,int b);

(

3

)求绝对值,和求最大值的方法基本相同。

public static int abs(int a)

(

4

)四舍五入的方法

public static int round(float a)

public static long round(double d)

(

5

)计算幂

public static double pow(double a,double b)

(

6

)求下限值

public static double floor(double d)

(

7

)求上限值

public static double ceil(double d)

(

8

)求平方根

public static double sqrt(double d)

下面的例子包含了上面的

8

个方法:

double

d1

=

5.7;

double

d2

=

12.3;

double d3 = -5;

System.out.println(d1+"

和

"+d2+"

的最大值为:

"+Math.max(d1,d2));

System.out.println(d1+"

和

"+d2+"

的最小值为:

"+Math.min(d1,d2));

System.out.println(d3+"

的绝对值为:

"+Math.abs(d3));

System.out.println(d2+"

四舍五入之后为:

"+Math.round(d2));

System.out.println(d2+"

的

2

次幂为:

"+Math.pow(d2,2));

System.out.println(d2+"

的下限为:

"+Math.floor(d2));

System.out.println(d2+"

的上限为:

"+Math.ceil(d2));

System.out.println(d2+"

的平方根为:

"+Math.sqrt(d2));

运行结果为:

5.7

和

12.3

的最大值为:

12.3

5.7

和

12.3

的最小值为:

5.7

-5.0

的绝对值为:

5.0

12.3

四舍五入之后为:

12

12.3

的

2

次幂为:

151.29000000000002

12.3

的下限为:

12.0

12.3

的上限为:

13.0

12.3

的平方根为:

3.5071355833500366

(

9

)要获取一个随机数,如果是

0

到

1

之间的随机数,可以直接使用下面的方法:

public static double random();

如果希望得到某个范围的随机数,例如

60

到

100

,可以这样处理:

int

min=60;

int

max=100;

int

random;

random = min + (int) ( (max - min) * (Math.random()));

Math.round(11.5)等于多少? Math.round(-11.5)等于多少?的更多相关文章

  1. Oracle_RAC数据库GI的PSU升级(11.2.0.4.0到11.2.0.4.8)

    Oracle_RAC数据库GI的PSU升级(11.2.0.4.0到11.2.0.4.8) 本次演示为升级oracle rac数据库,用GI的psu升级,从11.2.0.4.0升级到11.2.0.4.8 ...

  2. 【C++11】30分钟了解C++11新特性

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 什么是C++11 C++11是曾经被叫做C+ ...

  3. PSU 离11.2.0.3.0 -> 11.2.0.3.11 如果解决冲突的整个

    Oracle rdbms 扑灭psu离11.2.0.3.0升级到11.2.0.3.11 参考patch :18522512 停止应用,停止听音乐并DB,将db的oracle_home在下面OPatch ...

  4. 设正整数n的十进制表示为n=ak……a1a0(0<=ai<=9,0<=i<=k,ak!=0),n的个位为起始数字的数字的正负交错之和T(n)=a0+a1+……+(-1)kak,证明:11|n的充分必要条件是11|T(n);(整除理论1.1.2))

    设正整数n的十进制表示为n=ak……a1a0(0<=ai<=9,0<=i<=k,ak!=0),n的个位为起始数字的数字的正负交错之和T(n)=a0+a1+……+(-1)kak, ...

  5. AIX 7.1 RAC 11.2.0.4.0升级至11.2.0.4.6(一个patch跑了3个小时)

    1.环境 DB:两节点RAC 11.2.0.4.0升级至11.2.0.4.6 OS:AIX 7.1(205G内存 16C) 2.节点1.节点2(未建库) 2.1.patch 20420937居然用了3 ...

  6. 下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y),y++);

    下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...

  7. Transaction Check Error:file /usr/libexec/getconf/default conflicts between attempted installs of gcc-6.4.1-1.fc25.i686 and gcc-6.4.1-1.fc25.x86_64

    今天在我的ubuntu系统上使用yum来安装软件时出错了错误:Transaction Check Error:file /usr/libexec/getconf/default conflicts b ...

  8. 【ERROR】while loading shared libraries: /u01/app/oracle/product/11.2.0/lib/libclntsh.so.11.1: cannot

    问题: [oracle@mydb]$ lsnrctl status lsnrctl: error while loading shared libraries: /u01/app/oracle/pro ...

  9. lsnrctl: error while loading shared libraries: /opt/app/oracle/product/11.2/db_1/lib/libclntsh.so.11

    错误描述: 安装好数据库后,在oralce用户下敲入 查看监听状态命令,返回错误提示 [oracle@centos3 ~]$ lsnrctl statuslsnrctl: error while lo ...

  10. Downgrade ASM DATABASE_COMPATIBILITY (from 11.2.0.4.0 to 11.2.0.0.0) on 12C CRS stack.

    使用Onecommand完成快速Oracle 12c RAC部署后 发现ASM database compatibilty无法设置,默认为11.2.0.4.0. 由于我们还有些数据库低于这个版本,所以 ...

随机推荐

  1. 【转】SED多行模式空间

    在前面看到的都是单行模式.每次sed处理一个行. 但是sed是允许一次处理多行的.这就是所谓的多行模式空间. 多行模式空间命令有(N.D.P),他们分别对应单行模式空间(n.d.p). 分别是他们的多 ...

  2. 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法

    Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...

  3. Java中的null值总结

    自我总结,有什么不对或不到位的地方,请指出,感激不尽! 目的:熟练掌握java中null值出现的情况,避免NullPointerException 代码环境准备:需要引入junit;将代码packag ...

  4. Python学习笔记(一):列表和元组

    1.列表和元组的主要区别在于:列表可以修改,元组则不能.即如果要根据要求添加元素,列表更适合,    如果出于某种原因,序列不能修改的时候,使用元组更为合适. 2.通用序列操作1)索引:正数索引时,p ...

  5. vsftp 的安装及配置

    安装vsftpd 直接用 yum安装,方便的很 yum -y install vsftpd #进入vsftpd目录cd /etc/vsftpd/ #Create The Virtual Users D ...

  6. 让windows系统的DOS窗口也可以显示utf8字符集

    C:\Users\Administrator>chcp活动代码页: 936 windows cmd命令显示UTF8设置 在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CM ...

  7. InnoDB索引

    名词解释 clustered index(聚集索引) 对(primary key)主键索引的一种表述.InnoDB表存储是基于primary key列来组织的,这样做可以加快查询和排序速度.为了获得最 ...

  8. 有关static静态方法知识的收集

    1.何时使用静态方法: 如果某些操作不依赖具体实例,那它就是静态的,反之如果某些操作是依赖具体实例的(例如访问一个特定会员的名称),那它就应该是实例化的. 2.静态方法和实例方法的区别主要体现在两个方 ...

  9. git使用基本故障

    warning: LF will be replaced by CRLF in README.md. The file will have its original line endings in y ...

  10. gitlab项目迁移

    ALL Git* => Gitlab Nothing, Just copy the git URL to gitlab(类似于 fork) 使用 Git Mirror 無痛轉移 Git Serv ...