Java---数字处理类

这篇博客详细介绍了Java中数字处理的四个主要方面:数字格式化,包括DecimalFormat的使用;Math类的功能,如三角函数、指数函数和取整函数;随机数的生成,包括Math.random()方法和Random类的应用;以及大数字运算,重点讲解了BigInteger和BigDecimal类,支持任意精度的整数和定点数运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、数字格式化

二、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);

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 BigIntegeradd(BigInteger val);做加法运算
public BigIntegersubstract(BigInteger val);做减法运算
public BigIntegermultiply(BigInteger val)做乘法运算
public BigIntegerdivide(BigInteger val)做除法运算
public BigIntegerremainder(BigInteger val)做取余操作
public BigIntegerpow(int exponent);进行取参数的exponent次方操作
public BigIntegernegate();取相反数
public BigIntegershiftLeft(int n);将数字左移n位,若n为负数,则向右移
public BigIntegershiftRight(int n);将数字右移n位,若n为负数,则向左移
public BigIntegerand(BigInteger val);做与操作
public BigIntegeror(BigInteger val);做或操作
public BigIntegermin(BigInteger val);返回较小数值
public BigIntegermax(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 BigDecimaladd(BigDecimal augend);做加法操作
public BigDecimalsubstract(BigDecimal subtrahend);减法
public BigDecimalmultiply(BigDecimal multiplicand);乘法
public BigDecimaldivide(BigDecimal divisor, int scale, int roundingMode);除法

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值