数字处理类


1.数字格式化



在java中使用java.text.DecimalFormat格式化数字。

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

       如果数据绝对值大于0.001并且小于10000000,java将以常规小数形式表示。

       如果数据绝对值小于0.001或者大于10000000,使用科学计数法。

DecimalFormat是NumberFormat的一个子类,用于格式化十进制数字。


格式化模板中的特殊字符及其所代表的含义:



package hello;

import java.text.DecimalFormat;

public class Main {
	// 使用实例化对象时设置格式化模式
	static public void SimgleFormat(String pattern, double value) {
		
		DecimalFormat f1 = new DecimalFormat(pattern);// 实例化DecimalFormat对象
		String output = f1.format(value); // 将数字进行格式化
		System.out.println(value + " " + pattern + " " + output);
	}

	// 使用applyPattern()方法对数字进行格式化
	static public void UseApplyPatternMethodFormat(String pattern, double value) {
		
		DecimalFormat f2 = new DecimalFormat();// 实例化DecimalFormat对象
		f2.applyPattern(pattern); // 调用applyPatten()方法设置格式化模板
		System.out.println(value + " " + pattern + " " + f2.format(value));
	}

	public static void main(String[] args) {
		// 调用静态SimgleFormat()方法
		SimgleFormat("###,###.###", 123456.789); //123,456.789
		 // 在数字后加上单位
		SimgleFormat("00000000.#####kg", 123456.789);//00123456.789kg
		// 按照格式模板格式化数字,不存在的位以0显示
		SimgleFormat("000000.000", 123.78); //000123.780
		// 调用静态UseApplyPatternMethodFormat()方法
		// 将数字转换为百分数形式
		UseApplyPatternMethodFormat("#.###%", 0.789); //78.9%
		// 将小数点后格式化为两位
		UseApplyPatternMethodFormat("###.##", 123456.785); //123456.79
		// 将数字转化为千分数形式
		UseApplyPatternMethodFormat("0.00\u2030", 0.789); //789.00%
		
		//下面不用方法写的
		DecimalFormat f3=new DecimalFormat();
		//E后面的是指数形式,前面是底数的格式
		f3.applyPattern("#.###E00000");
		System.out.println(f3.format(1999.11)); //1999E00003
	}
}


使用setGroupingSize()与setGroupingUsed()方法设置数字格式。
package hello;

import java.text.DecimalFormat;

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

2.数学运算

package hello;

public class Main {
	public static void main(String[] args) {
		
		//1.三角函数方法
		 System.out.println(Math.sin(Math.PI/2)); //sin(π/2)=1.0
		 System.out.println(Math.cos(0)); //输出:1.0
		 System.out.println(Math.tan(Math.PI/4)); //输出:0.9999999999999999
		 // public static double asin(double x):传回x值的反正弦函数值。
		 // public static double acos(double x):传回x值的反余弦函数值。
		 //public static double atan(double x):传回x值的反正切函数值。
		 
		 //角度与弧度的转换通常不精确
		 //public static double toDegrees(double angrad):传回将弧度转换成角度 
		 System.out.println(Math.toDegrees(Math.PI/2)); //输出:90.0
		 //public static double toRadians(double angdeg): 传回将角度转换成弧度
		 System.out.println(Math.toRadians(90)); //输出:1.5707963267948966
		 System.out.println(Math.sqrt(9)); //输出:3.0
		 
		 //2.指数函数方法
		 System.out.println(Math.exp(0)); //输出: e的0次方=1.0
		 System.out.println(Math.log(1)); //输出: ln1=0.0
		 System.out.println(Math.log10(10)); //输出:取底数为10的对数:1.0
		 System.out.println(Math.sqrt(9)); //输出:9的平方根 3.0
		 System.out.println(Math.cbrt(8)); //输出:8的立方根 2.0
		 System.out.println(Math.pow(2, 3)); //输出:8.0
		 
		 //3.取整函数方法
		 System.out.println(Math.ceil(1.55)); //返回>=参数的最小整数:2.0
		 System.out.println(Math.floor(1.55)); //返回>=参数的最大整数:1.0
		 System.out.println(Math.rint(3.5)); //返回与参数最接近的整数,若同样则结果取偶数:4.0
		 //public static int round(float a)
		 //参数+0.5后 返回与参数最近的整数 即四舍五入
		 System.out.println(Math.round(3.4f));//输出:3
		 //public static long round(double a)
		 //参数+0.5后 返回与参数最近的整数 ,然后强制转换为长整型
		 System.out.println(Math.round(2.5));//输出:3
		 
		 //4.取最大值、最小值、绝对值。
		 //public static double max(double x,double y)
		 //public static int min(int a,int b)
		 //public static long min(long a,long b)
		 //public static float min(float a,float b) 参数为浮点型
		 //public static double min(double a,double 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) 
	}
}

3.随机数

(1)Math.random()方法

Math类中存在一个random()方法,用于产生随机数字,默认产生0.0~1.0的double型随机数,但是在Math.random()语句上稍加处理,就可以产生任意范围的随机数。



package hello;

public class Main {
	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));
	}
}


还可以随机生成字符

(char)('a'+Math.random()*('z'-'a'+1));    //随机生成a~z之间的字符

(char)(cha1+Math.random()*(cha2-cha1+1));   //随机生成cha1~cha2的字符

package hello;

public class Main {
	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 类

实例化Random对象创建一个随机数生成器:Random r=new Random();

要加import java.util.Random;

也可以在实例化的同时设置随机数生成器的种子:Random r=new Random(seedValue);

下面列举了获取各种数据类型随机数的方法:

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


4.大数字运算

BigInteger支持任意精度的整数,BigDecimal加入了小数的概念,支持任意精度的定点数,可以用来精确计算货币值.

(1) BigInteger

实例化BigInteger对象:public BigInteger(String val)  参数是以字符串形式存在的。

BigInteger b = new BigInteger("22");

常见运算方法:

add(BigInteger val): 加法        subtract(BigInteger val): 减法

multiply(BigInteger val): 乘法        divide(BigInteger val):除法

remainder(BigInteger val): 取余     pow(int exponent): 取exponent次方

negate(): 取相反数    int compareTo(BigInteger val): 数字比较

shiftLeft(BigInteger val): 将数字左移n位,如果n为负数,做右移操作

shiftRight(BigInteger val):  将数字右移n位,如果n为负数,做左移操作

and(BigInteger val): 与操作

or(BigInteger val): 或操作

boolean equals(Object x) 当参数x是BigInteger类型的数字并且数值相等时,返回true。

min(BigInteger val):  返回较小的数值       max(BigInteger val):  返回较大的数值

divideAndRemainder(BigInteger val): 用数组的形式返回,数组的第一个值为除法的商,第二个值为除法的余数

package hello;

import java.math.BigInteger;

public class Main {

	public static void main(String[] args) {

		BigInteger b1 = new BigInteger("6");
		BigInteger b2 = new BigInteger("2");
		System.out.println(b1.divideAndRemainder(b2)[0]);// 商:3
		System.out.println(b1.divideAndRemainder(new BigInteger("2"))[1]);// 余:0
	}
}

(2)  BigDecimal

构造方法:

public BigDecimal(double val)   将double表示形式转换为BigDecimal
public BigDecimal(int val)   将int表示形式转换为BigDecimal
public BigDecimal(String val)   将int表示形式转换为BigDecimal


常见方法:
public BigDecimal add(BigDecimal augend)   加法
public BigDecimal subtract(BigDecimalsubtrahend)   减法
public BigDecimal multiply(BigDecimalmultiplicand)   乘法
public BigDecimal divide(BigDecimaldivisor)   除法


package hello;

import java.math.BigDecimal;

class MyMath {
	public static double add(double d1, double d2) { // 进行加法运算
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.add(b2).doubleValue();
	}

	public static double sub(double d1, double d2) { // 进行减法运算
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.subtract(b2).doubleValue();
	}

	public static double mul(double d1, double d2) { // 进行乘法运算
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.multiply(b2).doubleValue();
	}

	public static double div(double d1, double d2, int len) {// 进行除法运算
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	public static double round(double d,int len) {     // 进行四舍五入操作
         BigDecimal b1 = new BigDecimal(d);
         BigDecimal b2 = new BigDecimal(1);
        // 任何一个数字除以1都是原数字
        // ROUND_HALF_UP是BigDecimal的一个常量,表示进行四舍五入的操作
        return b1.divide(b2, len,BigDecimal.ROUND_HALF_UP).doubleValue();
     }
}

public class Main {
	public static void main(String[] args) {
		System.out.println("加法运算:" + MyMath.round(MyMath.add(10.345, 3.333), 1));
		System.out.println("乘法运算:" + MyMath.round(MyMath.mul(10.345, 3.333), 3));
		System.out.println("除法运算:" + MyMath.div(10.345, 3.333, 3));
		System.out.println("减法运算:" + MyMath.round(MyMath.sub(10.345, 3.333), 3));
	}
}
/*输出结果
加法运算:13.7
乘法运算:34.48
除法运算:3.104
减法运算:7.012
*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值