java数字处理类常用方法汇总

今天学习了java的数字处理类相关内容,其中有很多常见方法,现将其整理方便记忆与查询
一.数字格式化
1.用java.text.DecimalFormat中的format()和applyFormat()方法,分别有两种格式化数字方法
第一种:实例化对象时直接设置数字格式化模板

DecimalFormat myFormat=new DecimalFormat(String pattern);
String output=myFormat.format(double value);

第二种:例化对象后再用applyPattern()设置数字格式模板

DecimalFormat myFormat=new DecimalFormat();
myFormat.applyPattern(String pattern);
String output=myFormat.format(double value);

2.设置将数字分组大小setGroupingSize();
设置是否支持分组setGroupingUsed();

二.数学运算
1.三角函数方法

System.out.println("90度的正弦值:"+Math.sin(Math.PI/2));
System.out.println("0度的余弦值:"+Math.cos(0));
System.out.println("60度的正切值:"+Math.tan(Math.PI/3));
System.out.println("2的平方根与2商的反余弦值:"+Math.acos(Math.sqrt(2)/2));
System.out.println("2的平方根与2商的反正弦值:"+Math.asin(Math.sqrt(2)/2));
System.out.println("1的反正切值:"+Math.atan(1));
System.out.println("120°的弧度值"+Math.toRadians(120));
System.out.println("π的角度值:"+Math.toDegrees(Math.PI));

2.指数函数方法

System.out.println("e的平方值:"+Math.exp(2));
System.out.println("以e为底的2的对数"+Math.log(2));
System.out.println("以10为底的2的对数值:"+Math.log10(2));
System.out.println("4的平方根值:"+Math.sqrt(4));
System.out.println("8的立方根值:"+Math.cbrt(8));
System.out.println("2的2次方值:"+Math.pow(2, 2));

3.取整函数方法

System.out.println("使用ceil()方法取整:"+Math.ceil(5.2));
System.out.println("使用floor()方法取整:"+Math.floor(2.5));
System.out.println("使用rint()方法取整:"+Math.rint(2.7));
System.out.println("使用rint()方法取整:"+Math.rint(2.5));
System.out.println("使用round()方法取整:"+Math.round(3.4f));
System.out.println("使用round()方法取整:"+Math.round(2.5));
System.out.println("4.2和8.5之间较大者:"+Math.max(4.2, 8.5));
System.out.println("4.4和4之间的最小者:"+Math.min(4.4, 4));
System.out.println("-7的绝对值:"+Math.abs(-7))    

三.随机数
1.Math.random()方法产生在0.0~1.0之间的随机数且得到的x满足0.0<=x<1.0

int s=(int)num1+(int)(Math.random()*(num2-num1));  //产生num1到num2之间的随机整数

2.Random类

Random r=new Random();
System.out.println("返回一个随机整数:"+r.nextInt());
System.out.println("返回大于等于0且小于n的随机整数:"+r.nextInt(5));
System.out.println("返回一个随机长整型值:"+r.nextLong());
System.out.println("返回一个随机boolean值:"+r.nextBoolean());
System.out.println("返回一个随机浮点型值:"+r.nextFloat());
System.out.println("返回一个随机双精度型值:"+r.nextDouble());
System.out.println("返回一个概率密度为高斯分布的双精度值:"+r.nextGaussian());

四.大数字运算
1.BigInteger类支持任何大小的整型运算

BigInteger bigInstance=new BigInteger("4");
System.out.println("加法操作:"+bigInstance.add(new BigInteger("2")));
System.out.println("减法操作:"+bigInstance.subtract(new BigInteger("2")));
System.out.println("乘法操作:"+bigInstance.multiply(new BigInteger("2")));
System.out.println("除法操作:"+bigInstance.divide(new BigInteger("2")));
System.out.println("取余操作:"+bigInstance.remainder(new BigInteger("2")));
System.out.println("返回余数和商操作(只显示余数):"+bigInstance.divideAndRemainder
                (new BigInteger("2"))[1]);
System.out.println("取参数5次方操作:"+bigInstance.pow(5));
System.out.println("取相反数:"+bigInstance.negate());
System.out.println("将数字左移-5位(右移5位):"+bigInstance.shiftLeft(-5));
System.out.println("将数字右移5位:"+bigInstance.shiftRight(5));
System.out.println("做与操作:"+bigInstance.and(new BigInteger("5")));
System.out.println("做或操作:"+bigInstance.or(new BigInteger("5")));
System.out.println("做数字比较操作:"+bigInstance.compareTo(new BigInteger("5")));

2.BigDecimal类支持任何精度的定点数运算
常用的有加、减、乘、除运算,与BigInteger类似,只是除法不同
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
方法中三个参数分别表示除数、商的小数点后的位数、近似处理模式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值