Java-Math
java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名: import static java.lang.Math.*;
这样在调用Math的方法时就可以简单地写出方法名,例如: cos(radians);
----------------------------------------------------------
1、基本方法: abs, max, min, ceil, floor, rint, round
重载abs方法,返回一个数(int、long、float、double)的绝对值
Math.abs(-30.5) == 30.5
Math.abs(-100.0989) == 100.0989
Math.ceil(30.4) == 31.0
Math.ceil(-8.0989) == -8.0
重载max和min方法,返回两个数(int、long、float、double)的最大值和最小值
public static double ceil(double x); //向上取整,返回double
public static double floor(double x); //向下取整,返回double
public static double rint(double x); //以double值返回与x最接近的整数,如果x到两个整数的距离相等,返回其中的偶数
public static long round(double x); //返回(long)Math.floor(x+0.5);
public static int round(float x); //返回(int)Math.floor(x+0.5);
2、指数和对数方法: (Math.E = 2.7183) exp, log, pow, sqrt
3、三角函数: sin, cos, tan, asin, acos, atan double atan2 (double y, double x); //将直角坐标系的坐标(x, y)转变为极坐标中的坐标(r, theta),并返回角度thera public static double toDegrees(double radians);
public static double toRadians(double degree);
4、随机数 0.0 <= Math.random() < 1.0 如果要得到一个[0, 10)之间的随机整数: int number = (int)(Math.random() * 10);
如果要得到一个[50, 100)之间的随机整数:
int number = 50 + (int)(Math.random() * 50);
如果要得到一个(a, a+b]之间的随机整数:
int number = a + (int)(Math.random() * b);
使用Math.Random()可以得到单个随机数,但若要得到一系列随机数,则可以使用java.util.Random 类,通过创建对象,调用相应的方法实现
Java-Math的更多相关文章
- java.math.RoundingMode 几个参数详解
java.math.RoundingMode里面有几个参数搞得我有点晕,现以个人理解对其一一进行总结: 为了能更好理解,我们可以画一个XY轴 RoundingMode.CEILING:取右边最近的整数 ...
- 【java.math.BigInteger】常用函数
1. /* 返回此BigInteger的函数正负号. 此方法返回-1,0或1作为此BigInteger的值对应是负,零或正数. */ java.math.BigInteger.signum(BigIn ...
- 【java.math.BigInteger】【转】常见问题
好大的链接给原作 Q: 在java怎样将BigInteger类型的数据转成int类型的? A:BigInteger的intValue()可以获得int类型数值. Q: java.math.BigInt ...
- [记录]java.math.biginteger cannot be cast to java.lang.long
可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...
- java.math.BigInteger使用心得总结(转)
今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换.都会变成0.参考书籍发现使用使用BigInteg ...
- java.math.BigDecimal类
BigDecimal类用于高精度计算.一般的float型和Double型数据只可以用来做科学计算或者是工程计算,由于在商业计算中,要求的数字精度比较高,所以要用到java.math.BigDecima ...
- java.math.BigDecimal()的用法
Java中简单的浮点数类型float和double是不能进行运算的,不光Java,很多语言都是这样. 我们运行下面程序你将会看到 public class TestMathDecimal { publ ...
- Java中的java.math.BigInteger
Java中的java.math.BigInteger /** * */ package com.you.model; /** * @author YouHaidong * */ public clas ...
- 数据转换失败 java.math.BigDecimal cannot be cast to java.lang.String
从接口获取到数据,在转换的时候出现错误:java.math.BigDecimal cannot be cast to java.lang.String 因为一开始用的是使用关键字进行强制转换,后来发现 ...
- [十五]java.math包简介,RoundingMode与MathContext
java.math包提供了java中的数学类 包括基本的浮点库.复杂运算以及任意精度的数据运算 '可以看得到,主要包括三个类一个枚举 BigDecimal和BigInteger接下来会详细介绍 先 ...
随机推荐
- .net mvc RazorEngine 字符串razor参数替换
在.net中有一个比较好的字符串参数替换的方案RazorEngine推荐大家看看原网站,然后做个小联系然后你就懂啦 首先呢得下载一个吧, vs中tools-> Library Paging Ma ...
- The request failed with HTTP status 401: Unauthorized.
Reporting Service 控件默认由IIS里面的应用程序池标识 里面所定义的用户连接,如果用户没有权限则报以下错误 The request failed with HTTP status 4 ...
- 模拟美萍加密狗--Rockey2虚拟狗(二)
按好了WDK,看了一天的DSF例子GenericHID,直接头大了,就能改个VID,PID让美萍识别成R2的狗.其他的什么各种描述符,根本无从下手,怪不得网上没有驱动模拟的加密狗,确实太复杂了,特别像 ...
- php浮点数计算比较及取整不准确解决方法
原文:php浮点数计算比较及取整不准确解决方法 php有意思的现象,应该是很多编程语言都会有这样的现象.这个是因为计算机的本身对浮点数识别的问题..... $f = 0.58; var_dump(in ...
- Oracle select into from 和 insert into select
select into from SQLSERVER 创建表: select * into aaa from bbb Oracle 创建表: create table aaa as select * ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...
- 阿里Android一面(校招)
4.8号晚上8点多接到了阿里一面的电话,面试官人很好,和我聊了半个多小时.我面的是无线事业部,就是做淘宝客户端的那个部门.面试问的都很基础,刚开始问了hashmap和快速排序.接着就是问Android ...
- PHP - 数组去重,(折中:符串去重)
[译]更快的方式实现PHP数组去重 Jan 11, 2016 • Hector 原文:Faster Alternative to PHP’s Array Unique Function 概述 使用PH ...
- 【Linux命令】查找命令
如果你想在当前目录下 查找"hello,world!"字符串,可以这样: grep -rn "hello,world!" *
- 将一个数转化为二进制后,求其中1的个数 C++
#include <iostream>using namespace std;int func(int x){ int count=0; while(x) { x=x&(x-1); ...