Java数字处理类

一、数字格式化

      在Java中没有格式化的数据遵循以下原则:

  • 如果数据绝对值大于0.001并且小于1000000, Java 将以常规小数形式表示。
  • 如果数据绝对值小于0.001或者大于0000000,使用科学记数法表示。

       DecimalFormat是NumberFormat的一个子类,用于格式化十进制数字。它可以将一些数字格式化为整数、浮点数、百分数等。通过使用该类可以为要输出的数字加上单位或控制数字的精度。一般情况下可以在实例化DecimalFormat对象时传递数字格式,也可以通过DecimalFormat类中的appyPattem()方法来实现数字格式化。当格式化数字时,在DecimalFormat类中使用一-些特殊字符构成-一个格式化模板,使数字按照一定的特殊字符规则进行匹配。在下表列举了格式化模板中的特殊字符及其所代表的含义。

例:

public class DecimalFormatSimpleDemo {
	// 使用实例化对象时设置格式化模式
	static public void SimgleFormat(String pattern, double value) {
		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();
		myFormat.applyPattern(pattern);
		String output = myFormat.format(value);
		System.out.println(value + " " + pattern + " " + output);
	}
	
	// 数字分组
	static public void GroupingSize() {
		DecimalFormat myFormat = new DecimalFormat();
		myFormat.setGroupingSize(2);
		String output = myFormat.format(123456.789);
		System.out.println("将数字以每两个数字分组:" + output);
		myFormat.setGroupingUsed(false);
		System.out.println("不允许个数字分组:" + output);
	}
		
	public static void main(String[] args) {
		// 调用静态SimgleFormat()方法
		SimgleFormat("###,###.###", 123456.789);
		// 在数字后加上单位
		SimgleFormat("00000000.###kg", 123456.789); 
		// 按照格式模板格式化数字,不存在的位以0显示
		SimgleFormat("000000.000", 123.78);
		// 调用静态UseApplyPatternMethodFormat()方法,将数字转换为百分数形式
		UseApplyPatternMethodFormat("#.###%", 0.789);
		// 将小数点后格式化为两位
		UseApplyPatternMethodFormat("###.##", 123456.789);
		// 将数字转化为千分数形式
		UseApplyPatternMethodFormat("0.00\u2030", 0.789);
		// 数字分组
		GroupingSize();
	}
}

输出:

 

二、数学运算

       Java中提供了一个执行数学基本运算的Math类,在Math类中的常用数学运算方法较多,大致可以将其分为4大类别,分别为三角函数方法、指数函数方法、取整函数方法以及取最大值、最小值和绝对值函数方法。除此之外还包含一些常用数学常量,如PI、E等。

可使用如下形式调用:
       Math.数学方法

1、三角函数方法

  • public static double sin(double a):返回角的三角正弦。
  • public static double cos(doublea):返回角的三角余弦。
  • public static double tan(doublea):返回角的三角正切。
  • public static double asin(double a):返回-一个值的反正弦。
  • public static double acos(doublea):返回-一个值的反余弦。
  • public static double atan(doublea):返回一个值的反正切。
  • public static double toRadians(double angdeg):将角度转换为弧度。
  • public static double toDegrees(double angrad);将弧度转换为角度。

 

2、指数函数方法

  • public static double exp(doublea):用于获取e的a次方。
  • public static double log(doublea):用于取自然对数,即取Ina的值。
  • public static double log10(doublea):用于取底数为10的对数。
  • public static double sqrt(doublea);用于取a的平方根,其中a的值不能为负值。
  • public static double cbrt(double a):用于取a的立方根。
  • public static double pow(double a,doubleb):用于取a的b次方。

 

3、取整函数方法

  • public static double ceil(double a):返回大于等于参数的最小整数。
  • public static double floor(double a):返回小于等于参数的最大整数。
  • public static double rnt(double a):返回与参数最接近的整数,如果两个同为整数且同样接近,则结果取偶数。
  • public static int round(float a):将参数加上0.5后返回与参数最近的整数。
  • public static long round(double a):将参数加.上0.5后返回与参数最近的整数,然后强制转换为长整型。

 

4、去最大值、最小值、绝对值函数方法

  • public static double max(double a,double b):取a与b之间的最大值。
  • public static int min(inta,intb);取a与b之间的最小值,参数为整型。
  • publie static long min(long a,longb):取a与b之间的最小值,参数为长整型。
  • public static float min(float a,float b):取a与b之间的最小值,参数为浮点型。
  • public static double min(double a,doubleb):取a与b之间的最小值,参数为双精度型。
  • public static int abs(int a):返回整型参数的绝对值。
  • public static long abs(long a):返回长整型参数的绝对值。
  • public static float abs(float a);返回浮点型参数的绝对值。
  • public static double abs(double a):返回双精度型参数的绝对值。

例:

// 取90度的正弦
System.out.println("90度的正弦值:" + Math.sin(Math.PI / 2));
// 取120度的弧度值
System.out.println("120度的弧度值:" + Math.toRadians(120.0));
// 取e的2次方
System.out.println("e的平方值:" + Math.exp(2)); 
// 取以e为底2的对数
System.out.println("以e为底2的对数值:" + Math.log(2));
// 将参数加上0.5后返回最接近的整数
System.out.println("使用round()方法取整:" + Math.round(3.4f));
// 取两个参数的最小值
System.out.println("4.4和4较小者:" + Math.min(4.4, 4));

输出:

 

三、随机数

1、Math.random()方法

       在Math类中存在-一个random0方法,用于产生随机数字,这个方法默认生成大于等于0.0且小于1.0的double型随机数,即0<=Math,random0<1.0,虽然Math.random0方法只可以产生0-1之间的double型数字,其实只要在Math.randomQ语句上稍加处理,就可以使用这个方法产生任意范围的随机数。

  • (int)(Math.random() * n):返回大于等于0且小于等于n的随机数
  • m + (int)(Math.random() * n):返回大于等于m且小于等于m+n(不含m+n)的随机数
  • (char)(‘a’ +Math.random() * (‘z’) - ’a’ +1 ):生成a~z之间字符

 

2、Random类

       除了Math类中的random0)方法可以获取随机数之外,Java中还提供了一种可以获取随机数的方式,那就是java.util.Random类。可以通过实例化一个Random对象创建一个随机数生成器。

       Random r = new Random();

       以这种方式实例化对象时,Java 编译器以系统当前时间作为随机数生成器的种子,因为每时每刻的时间不可能相同,所以产生的随机数将不同,但是如果运行速度太快,也会产生两次运行结果相同的随机数。同时也可以在实例化Random类对象时,设置随机数生成器的种子。语法如下:

       Random r = new Random(seedValue); // seedValue为随机数生成器的种子

在Random获取各种数据类型随机数的方法:

  • public int nextInt):返回-一个随机整数。
  • public int nextInt(int n):返回大于等于0且小于n的随机整数。
  • public long nextLong(:返回一个随机长整型值。
  • public boolean nextBoolean():返回- -个随机布尔型值。
  • public float nextFloat():返回一个随机浮点型值。
  • public double nextDouble():返回-一个随机双精度型值。
  • public double nextGaussian(:返回-一个概率密度为高斯分布的双精度值。

例:

// 2~15随机数
int num1 = 2, num2 = 15;
int s = (int) num1 + (int) (Math.random() * (num2 - num1));
System.out.println(s);
		
// a~z随机字符
char cha1 = 'a', cha2 = 'z';
char cha = (char) (cha1 + Math.random() * (cha2 - cha1 + 1));
System.out.println(cha);

输出:

 

四、大数字运算

       在Java中提供了大数字的操作类,即java.math.BigInteger类与java.math.BigDecimal 类。这两个类用于高精度计算,其中BigInteger类是针对大整数的处理类,而BigDecimal类则是针对大小数的处理类。

1、BigInteger

       BigInteger 支持任意精度的整数,也就是说在运算中BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息。使用BigInteger 类,可以实例化-一个BigInteger 对象,并自动调用相应的构造函数。BigInteger 类具有很多构造函数,但最直接的一种方式是参数以字符串形式代表要处理的数字。

       public BigInteger(String val)

val:十进制

BigInteger 运算:

  • public BigInteger add(BigInteger val):做加法运算。
  • public BigInteger subtract(BigInteger val):做减法运算。
  • public BigInteger multiply(BigInteger val):做乘法运算。
  • public BigInteger divide(BigInteger val):做除法运算。
  • public BigInteger remainder(BigInteger val):做取余操作。
  • public BigInteger[] divideAndRemainder(BigInteger val):用数组返回余数和商,结果数组中第
  • 一个值为商,第二个值为余数。
  • public BigInteger pow(int exponent):进行取参数的exponent次方操作。
  • public BigInteger negat):取相反数。
  • public BigInteger shifLef(intn);将数字左移n位,如果n为负数,做右移操作。
  • public BigInteger shiftRight(intn):将数字右移n位,如果n为负数,做左移操作。
  • public BigInteger and(BigInteger val):做与操作。
  • public BigInteger or(BigInteger val):做或操作。
  • public int compareTo(BigInteger val):做数字比较操作。
  • public boolean equals(Object x);当参数x是BigInteger类型的数字并且数值相等时,返回true.
  • public BigInteger min(BigInteger val):返回较小的数值。
  • public BigInteger max(BigInteger val):返回较大的数值。

 

2、BigDecimal

       BigDecimal类支持任何精度的定点数。常用的两个构造方法如下:

  • public BigDecimal(double val):实例化时将双精度型转换为BigDecimal类型。
  • public BigDecimal(String val):实例化时将字符串形式转换为BigDecimal类型。

       BigDecimal类型的数字可以用来做超大的浮点数的运算,如加、减、乘、除等,但是在所有的运算中除法是最复杂的,因为在除不尽的情况下末位小数点的处理是需要考虑的。

下面列举了BigDecimal类中实现加、减、乘、除的方法。

  • public BigDecimal add(BigDecimal augend):做加法操作。
  • publie BigDecimal subtract(BigDecimal subtrahend):做减法操作。
  • publie BigDecimal multiply(BigDecimal multiplicand):做乘法操作。
  • publie BigDecimal divide(BigDecimal divisor,int scale,int roundingMode): 做除法操作,方法中3个参数分别代表除数、商的小数点后的位数、近似处理模式。

       在上述方法中,BigDecimal类 中divideO方法有多种设置,用于返回商末位小数点的处理,这些模式的名称与含义下表所示。

例:

public class BigDecimalDemo {
	// 定义除法方法,参数分别为除数与被除数以及商小数点后的位数
	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));
		// 调用除法方法(b1 / b2),商小数点后保留b位,并将结果进行四舍五入操作
		return b1.divide(b2, b, BigDecimal.ROUND_HALF_UP);
	}
		
	public static void main(String[] args) {
		// 实例化一个大数字
		BigInteger bigInstance = new BigInteger("4"); 
		// 取该大数字除以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]);
		
		// 两个数字相除,保留小数后5位
		BigDecimalDemo b = new BigDecimalDemo();		
		System.out.println("两个数字相除,保留小数后5位:"+b.div(-7.5,8.9, 5));
	}
}

输出:

原创文章 99 获赞 68 访问量 4万+

猜你喜欢

转载自blog.csdn.net/King_weng/article/details/103655101
今日推荐