栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数字运算处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数字运算处理

数字格式化

public class DecimalFormatSimpleDemo {
	// 使用实例化对象时设置格式化模式
	static public void SingleFormat(String pattern, double value) {
		// 实例化DecimalFormat对象
		DecimalFormat myFormat = new DecimalFormat(pattern); 
		String output = myFormat.format(value); // 将数字进行格式化
		System.out.println(value + " " + pattern + " " + output);
	}
	
	// 使用applyPattern()方法对数字进行格式化
	static public void UseApplyPatternMethodFormat(String pattern, double value) {
		DecimalFormat myFormat=new DecimalFormat();//实例化DecimalFormat对象
		myFormat.applyPattern(pattern); // 调用applyPatten()方法设置格式化模板
		System.out.println(value + " " + pattern + " " + myFormat.format(value));
	}
	
	public static void main(String[] args) {
		//123,456.789
		SingleFormat("###,###.###", 123456.789);// 调用静态SimgleFormat()方法
		
		//00123456.789kg
		SingleFormat("00000000.###kg", 123456.789); // 在数字后加上单位
		
		//000123.780
		// 按照格式模板格式化数字,不存在的位以0显示
		SingleFormat("000000.000", 123.78);
		
		//78.9%
		// 调用静态UseApplyPatternMethodFormat()方法
		UseApplyPatternMethodFormat("#.###%", 0.789); // 将数字转换为百分数形式
		
		//123456.79
		// 将小数点后格式化为两位
		UseApplyPatternMethodFormat("###.##", 123456.789);
		
		//789.00%
		// 将数字转化为千分数形式
		UseApplyPatternMethodFormat("0.00u2030", 0.789);
	}
}
public class DecimalMethod {
	public static void main(String[] args) {
		DecimalFormat myFormat = new DecimalFormat();
		myFormat.setGroupingSize(2); // 设置将数字分组为2
		String output = myFormat.format(123456.789);
		//输出12,34,56.789
		System.out.println("将数字以每两个数字分组 " + output);
		myFormat.setGroupingUsed(false); // 设置不允许数字进行分组
		String output2 = myFormat.format(123456.789);
		//输出123456.789
		System.out.println("不允许数字分组 " + output2);
	}
}
Math 类

 三角函数

如果要求十分精确的结果,不建议使用Math类

public class TrigonometricFunction {
	public static void main(String[] args) {
		// 取90度的正弦
		System.out.println("90度的正弦值:" + Math.sin(Math.PI / 2));
		System.out.println("0度的余弦值:" + Math.cos(0)); // 取0度的余弦
		// 取60度的正切
		System.out.println("60度的正切值:" + Math.tan(Math.PI / 3));
		// 取2的平方根与2商的反正弦
		System.out.println("2的平方根与2商的反弦值:"
				+ Math.asin(Math.sqrt(2) / 2));
		// 取2的平方根与2商的反余弦
		System.out.println("2的平方根与2商的反余弦值:"
				+ Math.acos(Math.sqrt(2) / 2));
		System.out.println("1的反正切值:" + Math.atan(1)); // 取1的反正切
		// 取120度的弧度值
		// 角度变成弧度
		System.out.println("120度的弧度值:" + Math.toRadians(120.0));
		
		// 取π/2的角度
		// 弧度变成角度
		System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI / 2));
	}
}
 指数函数
public class ExponentFunction {
	public static void main(String[] args) {
		System.out.println("e的平方值:" + Math.exp(2)); // 取e的2次方
		// 取以e为底2的对数
		System.out.println("以e为底2的对数值:" + Math.log(2));
		// 取以10为底2的对数
		System.out.println("以10为底2的对数值:" + Math.log10(2));
		System.out.println("4的平方根值:" + Math.sqrt(4)); // 取4的平方根
		System.out.println("8的立方根值:" + Math.cbrt(8)); // 取8的立方根
		System.out.println("2的2次方值:" + Math.pow(2, 2)); // 取2的2次方
	}
}
 取整函数
public class IntFunction {
	public static void main(String[] args) {
		// 返回第一个大于等于参数的整数,向上取整
		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));
		// 将参数加上0.5后返回最接近的整数
		System.out.println("使用round()方法取整:" + Math.round(3.4f));
		// 将参数加上0.5后返回最接近的整数,并将结果强制转换为长整型
		System.out.println("使用round()方法取整:" + Math.round(2.5));
	}
}
 最大值,最小值,绝对值
public class AnyFunction {
	public static void main(String[] args) {
		System.out.println("4和8较大者:" + Math.max(4, 8)); 
		 // 取两个参数的最小值
		System.out.println("4.4和4较小者:" + Math.min(4.4, 4));
		System.out.println("-7的绝对值:" + Math.abs(-7)); // 取参数的绝对值
	}
}
随机数 Math.random()

public class MathRondom {
	
	public static int GetEvenNum(double num1, double num2) {
		// 产生num1~num2之间的随机数
		int s = (int) num1 + (int) (Math.random() * (num2 - num1)); 
		if (s % 2 == 0) { // 判断随机数是否为偶数
			return s; // 返回
		} else
			// 如果是奇数
			return s + 1; // 将结果加1后返回
	}
	
	public static void main(String[] args) {
		// 调用产生随机数方法
		System.out.println("任意一个2~32之间的偶数:" + GetEvenNum(2, 32));
	}
}
public class MathRandomChar {
	// 定义获取任意字符之间的随机字符
	public static char GetRandomChar(char cha1, char cha2) {
		return (char) (cha1 + Math.random() * (cha2 - cha1 + 1));
	}
	
	public static void main(String[] args) {
		// 获取a~z之间的随机字符
		System.out.println("任意小写字符" + GetRandomChar('a', 'z'));
		// 获取A~Z之间的随机字符
		System.out.println("任意大写字符" + GetRandomChar('A', 'Z'));
		// 获取0~9之间的随机字符
		System.out.println("0到9任意数字字符" + GetRandomChar('0', '9'));
	}
}
Random类

public class RandomDemo {
	public static void main(String[] args) {
		Random r = new Random(); // 实例化一个Random类
		// 随机产生一个整数
        // 这种方式可能返回正数 , 或者负数
		System.out.println("随机产生一个整数:" + r.nextInt());
		// 随机产生一个大于等于0小于10的整数
        //这种方式只能返回正数
		System.out.println("随机产生一个大于等于0小于10的整数:" + r.nextInt(10));
		// 随机产生一个布尔型的值
		System.out.println("随机产生一个布尔型的值:" + r.nextBoolean());
		// 随机产生一个双精度型的值
		System.out.println("随机产生一个双精度型的值:" + r.nextDouble());
		// 随机产生一个浮点型的值
		System.out.println("随机产生一个浮点型的值:" + r.nextFloat());
		// 随机产生一个概率密度为高斯分布的双精度值
		System.out.println("随机产生一个概率密度为高斯分布的双精度值:"
				+ r.nextGaussian());
	}
}
高精度运算

由于Math类计算精度不高,所以Java引入下面的类来进行高精度运算。

BigInteger 

高精度整数运算。

public class BigIntegerDemo {
	public static void main(String[] args) {
		BigInteger bigInstance = new BigInteger("4"); // 实例化一个大数字
		// 取该大数字加2的操作
		System.out.println("加法操作:" + bigInstance.add(new BigInteger("2")));
		// 取该大数字减2的操作
		System.out.println("减法操作:"
				+ bigInstance.subtract(new BigInteger("2")));
		// 取该大数字乘以2的操作
		System.out.println("乘法操作:"
				+ bigInstance.multiply(new BigInteger("2")));
		// 取该大数字除以2的操作
		System.out.println("除法操作:"
				+ bigInstance.divide(new BigInteger("2")));
		// 取该大数字除以3的商
		System.out.println("取商:"
				+ bigInstance.divideAndRemainder(new BigInteger("3"))[0]);
		// 取该大数字除以3的余数
		System.out.println("取余数:"
				+ bigInstance.divideAndRemainder(new BigInteger("3"))[1]);
		// 取该大数字的2次方
		System.out.println("做2次方操作:" + bigInstance.pow(2));
		// 取该大数字的相反数
		System.out.println("取相反数操作:" + bigInstance.negate());
	}
}
BigDecimal

高精度浮点数运算

public class BigDecimalDemo {
	static final int location = 10;
	
	
	public BigDecimal add(double value1, double value2) {
		// 实例化Decimal对象
		BigDecimal b1 = new BigDecimal(Double.toString(value1));
		BigDecimal b2 = new BigDecimal(Double.toString(value2));
		return b1.add(b2); // 调用加法方法
	}
	
	
	public BigDecimal sub(double value1, double value2) {
		BigDecimal b1 = new BigDecimal(Double.toString(value1));
		BigDecimal b2 = new BigDecimal(Double.toString(value2));
		return b1.subtract(b2); // 调用减法方法
	}
	
	
	public BigDecimal mul(double value1, double value2) {
		BigDecimal b1 = new BigDecimal(Double.toString(value1));
		BigDecimal b2 = new BigDecimal(Double.toString(value2));
		return b1.multiply(b2); // 调用乘法方法
	}
	
	
	public BigDecimal div(double value1, double value2) {
		return div(value1, value2, location); // 调用自定义除法方法
	}
	
	// 定义除法方法,参数分别为除数与被除数以及商小数点后的位数
	public BigDecimal div(double value1, double value2, int b) {
		if (b < 0) {
			System.out.println("b值必须大于等于0");
		}
		BigDecimal b1 = new BigDecimal(Double.toString(value1));
		BigDecimal b2 = new BigDecimal(Double.toString(value2));
		// 调用除法方法,商小数点后保留b位,并将结果进行四舍五入操作
		return b1.divide(b2, b, BigDecimal.ROUND_HALF_UP);
	}
	
	public static void main(String[] args) {
		BigDecimalDemo b = new BigDecimalDemo();
		System.out.println("两个数字相加结果:" + b.add(-7.5, 8.9));
		System.out.println("两个数字相减结果:" + b.sub(-7.5, 8.9));
		System.out.println("两个数字相乘结果:" + b.mul(-7.5, 8.9));
		System.out.println("两个数字相除结果,结果小数后保留10位:"+b.div(10, 2));
		System.out.println("两个数字相除,保留小数后5位:"+b.div(-7.5,8.9, 5));
	}
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348978.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号