Java---数字处理类

目录

一、数字格式化

二、Math类

三、随机数

四、大数字运算


一、数字格式化

Java主要对浮点类型数据(double型和float型)进行数字格式化操作。DecimalFormat是NumberFormat的一个子类,用于格式化十进制数字。需要:

import java.text.DecimalFormat;
DecimalFormat类中特殊字符说明
字符 说明
0 表示数字的阿拉伯数字,如果改为不存在数字,则显示0
# 表示数字的阿拉伯数字,如果该位存在数字,则显示字符;若该位不存在字符,则不显示
. 小数分隔符或货币小数分隔符
- 负号
, 分组分隔符
E 分隔科学记数法中的尾数和指数
% 放置在数字的前缀或后缀,将数字乘以100显示为百分数
\u2030 放置在数字的前缀或后缀,将数字乘以1000显示为千分数
\u00A4 放置在数字的前缀或后缀,作为货币符号
'(单引号)

当上述特殊字符出现在数字中,应添加单引号,系统则会将此符号作为普通符号处理

设置格式化模式方法:

1. DecimalFormat myFormat = new DecimalFormat(pattern);

2. DecimalFormat myFormat = new DecimalFormat();    
    myFormat.applyPattern(pattern);

扫描二维码关注公众号,回复: 2912919 查看本文章

3. DecimalFormat myFormat1 = new DecimalFormat();
    myFormat1.setGroupingSize(2);        

4. DecimalFormat myFormat3 = new DecimalFormat();
    myFormat3.setGroupingUsed(false);

import java.text.DecimalFormat;
public class decimalFormatSimpleDemo {
	static public void SimgleFormat(String pattern, double value){  
		
		//使用实例化对象时设置格式化模式,实例化DecialFormat对象
		DecimalFormat myFormat = new DecimalFormat(pattern);   
		
		String output = myFormat.format(value);
		System.out.println(value + " " + pattern + " " + output);
	}
	
	static public void UseApplyPatternMethodFormat(String pattern, double value) {
		
                //使用applyyPattern()方法对数字进行格式化
		DecimalFormat myFormat = new DecimalFormat();    
		myFormat.applyPattern(pattern);       
		
		String output = myFormat.format(value);
		System.out.println(value + " " + pattern + " " + output);
	}
	
	static public void SpecialTypeMethods(double value) {
		DecimalFormat myFormat1 = new DecimalFormat();
		myFormat1.setGroupingSize(2);    //特殊方法一
		
		System.out.println(value
                        +"  myFormat1.setGroupingSize(2)  "+myFormat1.format(value));
		
		DecimalFormat myFormat2 = new DecimalFormat();
		myFormat2.setGroupingUsed(false);    //特殊方法二
		
		System.out.println(value
                        +"  myFormat2.setGroupingUsed(true)  "+myFormat2.format(value));
		
		DecimalFormat myFormat3 = new DecimalFormat();
		myFormat3.setGroupingUsed(true);
		System.out.println(value
                        +"  myFormat3.setGroupingUsed(false)  "+myFormat3.format(value));
	}
	
	public static void main(String[] args) {
		String pattern1 = "###,###.###";
		Double d = 123456.789;
		SimgleFormat(pattern1, d);
		String pattern2 = "#########.###%";
		UseApplyPatternMethodFormat(pattern2, d);
		SpecialTypeMethods(d);
		
	}

}

运行结果:

二、Math类

1. 三角函数方法

2. 指数函数方法

3. 取整函数方法

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

三、随机数

1. Math.random() 方法---默认生成大于等于0.0小于1.0的double型随机数

还可以生成随机字符:

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

2. Random类

import java.util.Random;

可以通过实例化一个Random对象创建一个随机数生成器

Random r = new Random();

以这种方法实例化对象时,Java编译器以系统当前时间作为随机数产生器的种子(如果运行速度太快,可能会产生相同的随机数)。也可以在实例化Random对象时,设置随机数生成器的种子。

Random r = new Randomm(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(); 返回一个概率密度为高斯分布的双精度值
Random r = new Random();
int randInt = r.nextInt();

System.out.println(randInt);

四、大数字运算

1. BigInteger

BigInteger类型的数字范围欸较Integer类型的数字范围要大很多。Integer是int的包装类,int的最大值为2^31 - 1。BigInteger支持任意精度的整数。

import java.math.BigInteger;

使用BigInteger类,可以实例化一个BigInteger对象。最直接的一种方法是参数以字符串形式代表要处理的数字。

//将十进制2转换为BigInteger形式
BigInteger twoInstance = new BigInteger("2");
public BigInteger(String val);  //val为十进制字符串

BigInteger类常用的几种运算方法:

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

2. BigDecimal

BigInteger加入了小数,支持任何精度的定点数,数字精度高。

import java.math.BigDecimal;

1) 两个常用的构造方法

public BigDecimal(double val);  

public BigDecimal(String val);

public BigDecimal add(BigDecimal augend); 做加法操作
public BigDecimal substract(BigDecimal subtrahend); 减法
public BigDecimal multiply(BigDecimal multiplicand); 乘法
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode); 除法

在上述方法中,BigDecimal类中divide() 方法有多种设置,用于返回商末位小数点的处理。

具体实例代码后续再补。。。

猜你喜欢

转载自blog.csdn.net/qq_42182367/article/details/82018861
今日推荐