数字处理类

1.  数字格式化
JAVA 主要是对浮点数进行格式化
DecimalFormat 类用于格式化十进制数字。它可以将一些数字格式化为整数,浮点数,百分数等。通过使用该类可以为要输出的数字加上但单位或控制数字的精度。一般情况下可以在实例化对象时传递数字格式。也可以通过类中的方法 applyPattern() 方法来实现数字格式化。
public  class demo {
static  public  void simgleFormat(String pattern, double  value) {
DecimalFormat   myFormat= new DecimalFormat( pattern);
String output= myFormat.format( value);
System. out.println( value+ " 按规则:"+ pattern+ "格式化为:"+ " "+ output);
}
Static public void UseApplyPatternMethodFormat(String pattern,double value){
DecimalFormat   myFormat= new DecimalFormat();
myFormat.applyPattern(pattern);
System. out.println( value+ " 按规则:"+ pattern+ "格式化为:"+ " "+ output);
}
 
  public  static  void main(String[] args) {
simgleFormat( "%",0.23); //转换成百分数
//# 代表阿拉伯数字,如果存在数字则显示,若无则不显示
//0 代表阿拉伯数字,如果存在数字则显示,若无显示 0
//% 百分数
//\u2030 千分数,前缀和后缀决定符号的位置
//\u00A4 货币符号
simgleFormat( "00000000.########kg",123456789.23); //加上单位
simgleFormat( "#####.kg",0.23);
simgleFormat( "####.###",0.237887); //三位小数,小数位数的控制
simgleFormat( "0.00\u2030",0.23);
simgleFormat( "0.0\u00A4",0.23);
}
}
2. 其他设置
SETGROUPINGSIZE() 设置分组大小
SETGROUPINGUSED() 设置是否允许将数字进行分组
DecimalFormat   myFormat= new DecimalFormat();
myFormat.setGroupingSize(2);
String output= myFormat.format(12345678.9);
System. out.println( output);
myFormat.setGroupingUsed( false);
String output1= myFormat.format(12345678.9);
System. out.println( output1);
2.  数学运算
Math 类中提供众多数学函数方法,主要包括三角函数,指数函数方法,取整函数方法,取最大值,最小值以及平均值函数方法,都是 static 方法
Math. 数学方法
数学常量
Math.pi
Math.E
3.  随机数
方法一: Math 类的 random ()方法
这个方法默认生成大于等于 0.0 且小于 1.0 double 型随机数  
0<=Math.Random()<1.0
(int)(Math.random()*n)   随机生成大于等于 0 小于 n 的数
(int)(Math.random()*n)+(int) num1 随机生成大于等于 num1 小于 n+num1 的数
(int)(Math.random()*(num2-num1))+(int) num1
生成大于等于 num1 小于 num2-num1+num1=num2 的数 [num1,num2)
1.  num1 num2 的随机数 [num1,num2)
公式: int s=(int)num1+(int)(Math.random()*(num2-num1));
2.  生成 A Z 的随机字符
Char c=(char)( a +Math.Random()*( z - a +1));// 得任意字符:
[0+97,97+25+1)
[a,123)
Char c=(char)(cha1+Math.random()*(cha2-cha1+1));(ch1 cha2)
Ascll 码:
1. A   65
2. a    97
3. Z   90
4. z    122
方法二: Random 类提供的产生各种数据类型随机数
1. 实例化对象, Random r=new Random();
2. 设置种子, Random r=new Random(seedValue);
3. 调用方法
   1. 获取随机整数 nextInt()
   2. 获取有范围的整数 nextInt(10) [0,10)
   3. 获取随机长整数 nextLong()
   4. 获取随机布尔值 nextBoolean()
   5. 获取随机浮点数 nextFloat()
   6. 获取随机双精度浮点数   nextDouble()
   7. 获取概率密度为高斯分布的双精度值 nextGaussian()
3. 大数字运算
1.BigInteger
// 加法
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "75892358235923759023");
System. out.println( bigInstance . add( new BigInteger( "2")));
}
// 减法
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "75892358235923759023");
System. out.println( bigInstance. subtract( new BigInteger( "2")));
}
//乘法
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "222222222222222222");
System. out.println( bigInstance.multiply( new BigInteger( "2")));
}
// 除法
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "222222222222222222");
System. out.println( bigInstance. divide( new BigInteger( "2")));
}
// 取商
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "4");
System. out.println( bigInstance.divideAndRemainder( new BigInteger( "3"))[0]);
}
// 取余数
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "5");
System. out.println( bigInstance.divideAndRemainder( new BigInteger( "3")) [1])
//n 次方
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "5");
System. out.println( bigInstance.pow(3));
}
// 相反数
public  static  void main(String[] args) {
BigInteger bigInstance= new BigInteger( "5");
System. out.println( bigInstance.negate());
}
 
2.BigDecimal
常用在精密的商业计算,加入了小数概念
// 加法
public  static  void main(String[] args) {
demo test= new demo();
BigDecimal b3= test.add(7.65,6.76);
System. out.println( b3);
}
public  BigDecimal add( double  value1, double  value2) {
BigDecimal b1= new BigDecimal(Double. toString( value1));
BigDecimal b2= new BigDecimal(Double. toString( value2));
  return  b1 .add( b2 );}
// 减法
public  static  void main(String[] args) {
demo test= new demo();
BigDecimal b3= test.add(7.65,6.76);
System. out.println( b3);
}
public BigDecimal add( double  value1, double  value2) {
BigDecimal b1= new BigDecimal(Double. toString( value1)); //减数
BigDecimal b2= new BigDecimal(Double. toString( value2)); //被减数
  return  b1.subtract( b2);}
// 乘法
public  static  void main(String[] args) {
demo test= new demo();
BigDecimal b3= test.add(7.65,6.76);
System. out.println( b3);
}
public BigDecimal add( double  value1, double  value2) {
BigDecimal b1= new BigDecimal(Double. toString( value1)); //乘数
BigDecimal b2= new BigDecimal(Double. toString( value2)); //被乘数
  return  b1.multiply( b2);
}
// 除法
public  static  void main(String[] args) {
demo test= new demo();
BigDecimal b3= test.div(7.65,6.76,3);
System. out.println( b3);
}
public BigDecimal div( double  value1, double  value2, int  b) { //b是保留的小数位数
BigDecimal b1= new BigDecimal(Double. toString( value1)); //除数
BigDecimal b2= new BigDecimal(Double. toString( value2)); //被除数
return  b1.divide( b2, b,BigDecimal. ROUND_HALF_UP);
 
}

猜你喜欢

转载自www.cnblogs.com/cainame/p/10091969.html