java编程入门11 数字处理类

1.数字格式化

1.DecimalFormat类(用于格式化十进制数字)

1)DecimalFormat类是NumberFormat的子类,它可以将一些数字格式化为整数、浮点数、百分数等。通过使用该类可以为要输出的数字加上单位或控制数字的精度。一般情况下可以在实例化DecimalFormat对象时传递数字格式,也可以通过DecimalFormat类中的applyPattern()方法来实现数字的格式化

2)在格式化数字时,DecimalFormat类中使用一些特殊字符构成一个格式化模板,使数字按照一定的特殊字符规则进行匹配

3)DecimalFormat类中的特殊字符说明

字符 说明
0 代表阿拉伯数字,如果该位不存在数字,则显示0
# 代表阿拉伯数字,如果该位存在数字,则显示字符;如果该位不存在数字,则不显示
. 小数分隔符或货币小数分隔符
- 负号
分组分隔符
E 分隔科学计数法中的尾数与指数
% 放置在数字的前缀或后缀,将数字乘以100显示为百分数
\u2030 放置在数字的前缀或后缀,将数字乘以1000显示为千分数
\u00A4 放置在数字的前缀或后缀,代表货币标记
' 当上述特殊字符出现在数字中时,应为特殊字符添加单引号,系统会将此字符视为普通字符处理

4)常用方法

1.setGroupingSize()方法设置格式化数字的分组大小

2.setGroupingUsed()方法设置是否可以对数字进行分组操作

public class Myclass {
    public static void SingleFormat(String pattern,double value) {
        DecimalFormat myFormat =new DecimalFormat(pattern);
        String output=myFormat.format(value);
        System.out.println(value+"  "+pattern+"  "+output);
    }
    public static void UseApplyMethodFormat(String pattern,double value) {
        DecimalFormat myFormat=new DecimalFormat();
        myFormat.applyPattern(pattern);
        String output=myFormat.format(value);
        System.out.println(value+"  "+pattern+"  "+output);
    }
    public static void main(String[] args) {
        SingleFormat("0.###kg",1.123);
        SingleFormat("00.00",1.126);
        UseApplyMethodFormat("##.##", 1.125);
        UseApplyMethodFormat("#.##%",0.75);

        DecimalFormat myFormat =new DecimalFormat();
        myFormat.setGroupingSize(2);
        String output=myFormat.format(123456.789);
        System.out.println(output);
        myFormat.setGroupingUsed(false);
        String output1=myFormat.format(123456.789);
        System.out.println(output1);
    }
}

 

注:

1)在这里值得一提的是Java的精确度的计数保留法默认为HALF_EVEN,向最接近的数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。(银行家舍入法)

如果舍弃部分左边的数字为奇数,则舍入行为与 ROUND_ HALF_UP 相同;

如果为偶数,则舍入行为与 ROUND_ HALF_DOWN 相同。,

不妨试一试SingleFormat("#",7.5);SingleFormat("#",8.5);与我们常用的四舍五入不同,如果想要改变java的精确度的计数保留法的模式,需要使用

ob(DecimalFormat对象).setRoundingMode(RoundingMode.HALF_UP);

2)改变十进制数的输出格式还可以使用String.Format()来实现

String str=String.format("%.4f",3.1415926);
System.out.println(str);

 

2.数学运算

1.在Math类中提供了众多数学函数方法,主要包括三角函数方法、指数函数方法、取整函数方法、取最大值、最小值以及平均值函数方法,这些方法都被定义为static形式。在Math类中还提供了一些常用的数学常量。如PI,E等

2.三角函数

1)public static double sin(double a): 返回角的三角正弦

2)public static double cos(double a): 返回角的三角余弦

3)public static double tan(double a): 返回角的三角正切

4)public static double asin(double a): 返回一个值的反正弦

5)public static double acos(double a): 返回一个值的反余弦

6)public static double atan(double a): 返回一个值的反正切

7)public static double toRadians(double angdeg): 将角度转换为弧度

8)public static double toDegrees(double angrad): 将弧度转换为角度

注:角都与弧度之间的转换通常是不精确的

3.指数函数

1) public static double exp(double a) :用于获取e的a次方

2) public static double log(double a) :用于取自然对数,即lna的值

3) public static double log10(double a) :用于取底数为10的对数

4) public static double sqrt(double a) :用于取a的平方根

5) public static double cbrt(double a) :用于取a的立方根

6) public static double pow(double a,double b) :用于取a的b次方

4.取整函数

1) public static double ceil(double a) :返回大于等于参数的最小整数

2) public static double floor(double a) :返回小于等于参数的最大整数

3) public static double rint(double a) :返回与参数最接近的整数,如果两个同为整数且同样接近,则结果却偶数

4) public static int round(float a) :将参数加入0.5后向下取整

5) public static long round(double a) :将参数加入0.5后向下取整,然后强制转换为长整型

5.取最大值、最小值、绝对值函数

1) public static double max():取两个数的最大值(int\long\double\float)

2) public static double min():取两个数的最小值(int\long\double\float)

3) public static double abs():取绝对值(int\long\double\float)

3.随机数

在java中主要提供了两种方式产生随机数

1.Math.random()方法

1.这个方法默认生成大于等于0.0且小于1.0的double型随机数

2.使用Math类的random方法稍加处理也可以生成其他随机数,比如生成A~Z之间的随机字符

(char) ('a'+Math.random()*('z'-'a'+1));

2.Random类

1.通过实例化一个Random对象创建一个随机数生成器,java编译器是以系统当前时间作为随机数种子

2.也可以在实例化Random对象时,设置随机数生成器的种子

Random r=new Random(seedValue)

3.Random类提供了获取各种数据类型随机数的方法

1) public int nextInt() :返回随机整数

2) public int nextInt(int n) :返回大于等于0且小于n的随机整数

3) public long nextLong() :返回随机长整型值

4) public boolean nextBoolean() :返回随机布尔型值

5) public float nextFloat() :返回随机浮点型值

6) public double nextDouble() :返回随机双精度型值 、

7) public double nextGaussian() :返回概率密度为高斯分布的双精度值

4.大数运算

1.java提供了大数字的操作类,即java.math.BigInteger类与java.math.BigDecimal类,这两个类用于高精度计算,其中BigInteger类针对大整数的处理类,而BigDecimal类针对大小数的处理类。

2.BigInteger

1.构造函数

最直接的方式是参数以字符串的形式代表要处理的数字

public BigInteger(String val)

2.运算方法

1) public BigInteger add(BigInteger val) :加法

2) public BigInteger subtract(BigInteger val) :减法

3) public BigInteger multiply(BigInteger val) :乘法

4) public BigInteger divide(BigInteger val) :除法

5) public BigInteger remainder(BigInteger val) :取余

6) public BigInteger[] divideAndRemainder(BigInteger val) :用数组保存商和余数,结果数组中第一个值为商,第二个为余数

7) public BigInteger pow(int exponent) :进行去参数的exponent次方操作

8) public BigInteger negate() :去相反数

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

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

11) public BigInteger and(BigInteger val) :做与操作

12) public BigInteger or(BigInteger val) :做或操作

13) public BigInteger compareTo(BigInteger val) :做数字比较操作

14) public BigInteger equals(Obeject x) :当参数x是BigInteger类型的数字并且数值相等时,返回true

15) public BigInteger min(BigInteger val) :返回较小的数值

16) public BigInteger max(BigInteger val) :返回较大的数值

3.BigDecimal

1.构造方法

在BigDecimal类中常用的构造方法如下

1)public BigDecimal(double val)

2)public BigDecimal(String val)

2.常用方法 1) public BigDecimal add(BigDecimal augend) :加法

2) public BigDecimal subtract(BigDecimal subtrahend) :减法

3) public BigDecimal multiply(BigDecimal multiplicand) :乘法

4) public BigDecimal add(BigDecimal divisor,int scale,int roundingMode) :除法 三个参数分别代表除数,商的小数点后的位数,近似处理模式

 

 

2018-12-19

posted @ 2018-12-19 18:16  CCSU_CBG  阅读(258)  评论(0编辑  收藏  举报