前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java中的数字类解析(包括格式化数字、大数运算等等)

Java中的数字类解析(包括格式化数字、大数运算等等)

作者头像
glm233
发布2020-09-28 11:16:07
1.2K0
发布2020-09-28 11:16:07
举报
  • 格式化数字
  • 掌握math类中的各种数学运算方法
  • 生成任意范围随机数
  • 掌握大整数和大小数的数字运算方式
  • 格式化数字

Java中如果数据绝对值大于0.001而小于10000000用常规小数表示,否则采用科学计数法表示

这就可能引起了一些不便,有时不能满足解决实际问题的需求,对此就引出了格式化数字的概念

在Java中采用java.text.DecimalFormat类对数字进行格式化操作,下面给出一个实例

在这里插入图片描述
在这里插入图片描述
import java.text.*;

public class DecimalFormatSimpleDemo {
	// 使用实例化对象时设置格式化模式
	static public void SimpleFormat(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) {
		SimpleFormat("###,###.###", 123456.789);// 调用静态SimgleFormat()方法
		SimpleFormat("00000000.###kg", 123456.789); // 在数字后加上单位
		// 按照格式模板格式化数字,不存在的位以0显示
		SimpleFormat("000000.000", 123.78);
		// 调用静态UseApplyPatternMethodFormat()方法
		UseApplyPatternMethodFormat("#.###%", 0.789); // 将数字转换为百分数形式
		// 将小数点后格式化为两位
		UseApplyPatternMethodFormat("###.##", 123456.789);
		// 将数字转化为千分数形式
		UseApplyPatternMethodFormat("0.00\u2030", 0.789);
	}

}

DecimalFormat类中对数字格式化设置的特殊方法

  • setGroupingSize(long)
  • setGroupingUsed(boolean)
import java.text.*;

public class DecimalMethod {
	public static void main(String[] args) {
		DecimalFormat myFormat = new DecimalFormat();
		myFormat.setGroupingSize(2); // 设置将数字分组为2
		String output = myFormat.format(123456.789);
		System.out.println("将数字以每两个数字分组 " + output);
		myFormat.setGroupingUsed(false); // 设置不允许数字进行分组
		String output2 = myFormat.format(123456.789);
		System.out.println("不允许数字分组 " + output2);
	}
}
  • 数学运算

Java中有个数学类,提供了众多数学函数方法和常用的常数,调用无非两种方式~

1.Math.数学方法
e.g Math.sin(double a)
2.Math.常量
e.g Math.PI

一些案例大家自己看看吧,写的很全了QwQ~

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));
		//round函数是四舍五入的
		System.out.println("使用round()方法取整:" + Math.round(2.5f));
	}
}
  • 最大值、最小值、绝对值
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)); // 取参数的绝对值
	}
}
  • 随机数

1.Math.random方法

在Math类中存在一个random方法,用于产生随机数字,范围是0~1.0,左闭右开,基于这个最基础的方法我们理论上可以产生出任意数字范围的随机数和任意两个字符范围之间的随机数

例如,产生任意两个数字之间的所有随机数

long a=(long)num1+(long)(Math.random()*(num2-num1);//产生随机数,范围是num1~num2,左闭右开

产生任意两个字符之间的随机字符

char ans=(char)char1+Math.random()*(char2-char1+1)//区间左端点和右端点都可以取到

Demo

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'));
	}
}

2.Random类

另一种方法是通过java.util.Random类实例化一个对象产生随机数生成器

也有两种写法

Random r=new Random();

Random r=new Random(seedValue);

Demo

import java.util.*;

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());
	}
}
  • Java中的大数运算

来,终于说到重点了,说到大数运算,这个功能可真的是求之不得,想当年答主在ACM之路上因为大数这个问题学习了半天,如今学了Java,不是迎刃而解了吗?虽说Java运行时间那是真的慢,而且也不常用来写这种算法题,可他依然牛逼hh,因为他有一个BigInteger类~

BigInteger和BigDecimal都支持大数运算,所不同的是后者加入小数的概念,具体还是用代码展示给大家吧

import java.math.*;

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());
		BigInteger a=new BigInteger("2");

	}
}

多说一嘴,public BigInteger[] divideAnReminder(BigInteger val)这个函数是用数组作为返回值,见名知意,第一个值为商,第二个是余数

OK,至此我们就学完了Java中的数字类的基本操作

看完再赞已成习惯,对您有助点点关注~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档