前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 数学操作类

Java 数学操作类

作者头像
Mirror王宇阳
发布2020-11-12 11:18:37
5550
发布2020-11-12 11:18:37
举报
文章被收录于专栏:Mirror的技术成长

数学操作类

Math类 数学计算操作类

类属性值

  • Math.E ^
  • Math.PI 圆周率

类方法

Math类中,一切方法都是 static 型,因为Math类中没有普通属性。

round() 方法
  • 四舍五入,返回最接近int值的参数
代码语言:javascript
复制
public static int round(float a)
abs() 方法
  • 返回绝对值
代码语言:javascript
复制
public static double abs(double a)
max() 方法
  • 返回int值中较大的那个值
代码语言:javascript
复制
public static int max(int a , int b)

Random类 随机操作类

  • java.util 包中
Random() 构造
  • 创建一个新的随机数生成器
next() 方法
  • 生成下一个伪随机数
代码语言:javascript
复制
protected int next (int bits)
nextInt() 方法
  • 返回下一个伪随机数
  • nextInt(int n)
    • 返回 小于 n之内的随机数
36选7 彩票器实例
代码语言:javascript
复制
import java.util.Random;

public class TestDemo {
	public static void main(String [] args) throws CloneNotSupportedException {
		Random ran = new Random();
		int data[] = new int [7] ; //开辟一个数组
		int foot = 0 ;
		while (foot < 7) {
			int t = ran.nextInt(37); // 随机生成返回一个不大于37的数
			if (!isRepeat(data,t)) { // 查重
				data[foot ++] = t ;
			}
		}
		java.util.Arrays.parallelSort(data);
		for (int x = 0 ; x < data.length ; x ++) {
			System.out.print(data[x] + "\t");
		}
	}
	public static boolean isRepeat(int temp[] , int num) { // 查重
		if (num == 0 ) {
			return true ;
		}
		for (int x = 0 ; x < temp.length ; x ++) {
			if (temp[x] == num) {
				return true ;
			}
		}
		return false;
	}
}

大数字操作类

BigInteger 类

代码语言:javascript
复制
import java.math.BigInteger;
import java.util.Random;

public class TestDemo {
	public static void main(String [] args) throws CloneNotSupportedException {
		BigInteger big_A = new BigInteger("12345678912345678912356789");
		BigInteger big_B = new BigInteger("218372948729847298347289") ;
		System.out.println("加法操作 >>> " + (big_A.add(big_B)));
		System.out.println("减法操作 >>> " + (big_A.subtract(big_B)));
		System.out.println("乘法操作 >>> " + (big_A.multiply(big_B)));
		System.out.println("除法操作 >>> " + (big_A.divide(big_B)));
	}
}
  • 运行结果:
代码语言:javascript
复制
加法操作 >>> 12564051861075526210704078
减法操作 >>> 12127305963615831614009500
乘法操作 >>> 2695962308160819899591376721692771747399598895021
除法操作 >>> 56

BigDecimal : 大浮点数

  • BigInteger只可以保存整数,不可以保存小数(浮点数),而BigDecimal可以保存小数(浮点)数据;在BigDecimal类提供如下构造:
代码语言:javascript
复制
public BigDecimal(String val) ;
public BigDecimal(double val) ; 

Math.round()方法虽然实现四舍五入操作,但是,小数在计算的时候会自动的四舍五入

除法操作
代码语言:javascript
复制
public BigDecimal divide(BigDecimal divisor , int scale , int round);
  • BigDecimal divsor : 被除数
  • int scale:保留的小数位
  • int round:进位模式
  • 实例:*【重要内容】
代码语言:javascript
复制
import java.math.BigDecimal;

class MyMath {
	/**
	 * 实现准确的位数的四舍五入操作
	 * @param num 进行四舍五入操作的数字
	 * @param scale 要保留的小数位数
	 * @return 处理后的数据
	 */
	public static double round(double num , int scale) {
		BigDecimal bigA = new BigDecimal(num) ;
		BigDecimal bigB = new BigDecimal(1) ;
		// ROUND_HALF_UP:向“最接近的”数字舍入
		// doubleValue() : 转 double 型数据
		return bigA.divide(bigB, scale , BigDecimal.ROUND_HALF_UP).doubleValue() ;
	}
}

public class TestDemo {
	public static void main(String [] args) {
		System.out.println(MyMath.round(19.224532 , 2));
		System.out.println(MyMath.round(3.1465926 , 2));
	}
}

总结:

  • Math类要清楚round()方法的缺陷
  • Random类生成随机数
  • 如果处理大量的数据量,则使用 BigInteger和BigDecimal ,两个类都属于Number的子类
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-06-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数学操作类
    • Math类 数学计算操作类
      • 类属性值
      • 类方法
    • Random类 随机操作类
      • 大数字操作类
        • BigInteger 类
        • BigDecimal : 大浮点数
        • 总结:
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档