DecimalFormat类
数字的格式化在解决实际问题时,使用非常普遍,如超市的商品价格,银行的余额显示,都需要对小数的精度有特定的要求,java主要对浮点数进行格式化.JAVA语言就是通过DecimalFormat类来对其进行格式化操作的.
DecimalFromat类是继承与NumberFormat类的,NumberFormat类又是Format类的子类,这里我们就可以很明显的观察到java类的继承机制是如何在Java类库中存在的.
public class DecimalFormat extends NumberFormat {
DecimalFormat类用以格式化十进制数字.他可以将一些数字格式化为整数,浮点数,百分数等.通过该类可以为要输出的数字加上单位或控制数字的精度.一般通过实例化DecimalFormat对象是传递数字格式,也可以通过类中的applyPattern方法来实现数字的格式化.
需要注意的是:
在格式化数字的时候,DecimalFormat类中使用了一些特殊字符构成一个格式化的模版,使数字按照一定的特殊字符来进行匹配.
// 第一种格式化方法,直接通过构造方法传入格式控制参数patten.
private void NumFormating(String patten, double adouble){ // 传入两个参数,第一个控制格式,第二个为传入的数值.
DecimalFormat format = new DecimalFormat(patten);
System.out.println(format.format(adouble)); // 通过format方法格式化参数adouble
}
// 第二种格式化方法,通过类的方法applyPattern方法传入格式控制参数patten
private void NumFormating1(String patten,double adouble){
DecimalFormat format = new DecimalFormat();
format.applyPattern(patten);
System.out.println(format.format(adouble)); // 通过format方法,格式化参数adouble
}
public static void main(String[] args) {
Sty_DigitalProcessing digitalProcessing = new Sty_DigitalProcessing();
digitalProcessing.NumFormating("000,000.0000",12345.67);
digitalProcessing.NumFormating1("###,###.####",12345.67);
digitalProcessing.NumFormating("###,###.####kg",12345.67);
System.out.println();
digitalProcessing.NumFormating("#.###%",0.345);
digitalProcessing.NumFormating("0.000%",0.345);
System.out.println();
digitalProcessing.NumFormating("0.0000\u2030",0.123456);
digitalProcessing.NumFormating("#.####\u2030",0.123456);
System.out.println();
digitalProcessing.NumFormating("000,000,000.0000\u00A4",1234567.89);
digitalProcessing.NumFormating("###,###,###.##'#'",1234567.89);
}
我们可以很明显的看出来,0和#之间的区别,前者,在位数不足时会补零,后者则不会补零.通常情况下,我们知道,一般是不会在数字前补零的,补零的情况通常出现在小数后面.
Math中常用的方法
Math中常用的数学运算方法有很多,其大致可以分为4类.三角函数方法,指数函数方法,取整方法以及取最大最小值和绝对值函数方法.
三角函数方法
它们都是静态的,所以可以直接通过类名.方法名调用.
System.out.println(Math.sin(subtense)); // 返回正弦
System.out.println(Math.cos(subtense)); // 返回余弦
System.out.println(Math.tan(subtense)); // 返回正切
System.out.println(Math.asin(subtense)); // 返回一个值的反正弦
System.out.println(Math.acos(subtense)); // 返回一个值的反余弦
System.out.println(Math.atan(subtense)); // 返回一个值的反正切
System.out.println(Math.toRadians(angdeg)); // 将角度转换为弧度
System.out.println(Math.toDegrees(angdeg)); // 将弧度转换为角度
// 指数函数方法
private void styindex(double adouble,double adouble1){
System.out.println(Math.exp(adouble)); // 用于获取e的a次方
System.out.println(Math.log(adouble)); // 用于取自然对数,lna的值
System.out.println(Math.log10(adouble)); // 用于取底数为10的整数
System.out.println(Math.sqrt(adouble)); // 用于取a的平方根,其中a的值不能为负值.
System.out.println(Math.cbrt(adouble)); // 用于取a的立方根
System.out.println(Math.pow(adouble,adouble1)); // 用于取a的b次方
}
private void styAdjustment(double adouble,float afloat){
System.out.println(Math.ceil(adouble)); // 返回大于等于参数的最小整数.
System.out.println(Math.floor(adouble)); // 返回小于等于参数的最大整数.
System.out.println(Math.rint(adouble)); // 返回与参数最接近的整数(若为0.5取偶数,即加一)
System.out.println(Math.round(adouble)); // 将参数家上0.5返回与参数最近的整数
System.out.println(Math.round(afloat)); // 将参数加上0.5后返回与参数最近的整数,然后强转为长整型.
}
/**
* 取最大值,最小值,绝对值函数方法.
* 其方法都重载有多个类型的,满足不同类型参数的最大最小值,绝对值的获取.
*/
private void stytakequals(double adouble,double adouble1){
System.out.println(Math.max(adouble,adouble1)); // 取两个double型参数的最大值
System.out.println(Math.min(adouble,adouble1));
System.out.println(Math.abs(adouble)); // 返回double型参数的绝对值
}
/**
* 随机数
* Math类中有提供random方法来生产随机数.
* 该方法默认生成0.0-1.0之间的double型随机数.
* 但该方法通过处理后还是可以生成任何范围的随机数的.
* ;将该方法*n后强转为int型输出,可以得到0-n范围的随机数.
* 而m+(int)Math.random()*n,将上面的式子+m后,就可以得到m-(m+n)范围的随机数.
*/
private void styrandom(){
System.out.println(Math.random());
System.out.println((int)(Math.random()*100));
System.out.println((int)(10+Math.random()*15));
System.out.println((char)('a'+ Math.random()*('z'-'a'+1))); // 这里其实可以封装成一个获取固定范围的随机字符的方法.
}
/**
* Random类
* Random类提供各种类型随机数的构造方法
* 通过实例化对象调用.
* 比Math.random更为强大.
*/
private void styRandom(int n){ // 传入参数n 控制范围
Random random = new Random(10000);
System.out.println(random.nextInt());
System.out.println(random.nextDouble());
}
关于random seedvalue的说明参看:
BigInteger类通过构造方法来直接将某个值转换为BigInteger类型.其构造方法有很多.
private void styBigInteger(){
BigInteger bigInteger = new BigInteger("12345678901234567890"); // 直接通过构造法方法,将值转为BigInteger型.
BigInteger bigInteger1 = new BigInteger("43534645376574567314253543");
System.out.println(bigInteger.add(bigInteger1)); // 简单的运算
}
BigDecimal bigDecimal = new BigDecimal(1231234345634.546756734563);
BigDecimal bigDecimal1 = new BigDecimal("124235345645765.783452352342534");
关于java数字处理类就了解到这么多了,其实其中还有很多方法都没有涉及到,关于java的类库,其中值得学习的,涵盖着各个方面的类和方法,需要在之后的不断学习的过程中不断接触了解才能够渐渐掌握,这里也仅仅只是对其中极少的一部分基本知识做总结学习.
最后也祝愿大家都能够在自己前进的道路上披荆斩棘,共同进步,走向成功.
更新时间:
2019-4-10
22:06
原文:https://www.cnblogs.com/gemuxiaoshe/p/10686551.html