Java学习随笔——数字类处理

1.数字格式化
数字格式化操作主要针对的是浮点型数据,包括double型和 float型数据。在Java中可以使用DecimalFormat类进行格式化操作
没有格式化的数据:
a.数据绝对值大于0.001并且小于10000000,使以常规小数形式表示。
b.数据绝对值小于0.001或者大于10000000,使用科学记数法表示

(1).DecimalFormat是NumberFormat的一个子类,用于格式化十进制数字,通过使用该类可以为要输出的数字加上单位或控制数字的精度。
DecimalFormat类中特殊字符说明:
在这里插入图片描述

import java.text.DecimalFormat;

public class DecimalFormatSimpleDemo{
    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+" "+myFormat.format(value));
    }
    public static void main(String[] args){
        SimgleFormat("###,###,###",123456.789);
        SimgleFormat("00000000.###kg",123456.789);
        SimgleFormat("000000.000",123.78);
        UseApplyPatternMethodFormat("#.###%",0.789);
        UseApplyPatternMethodFormat("###.##",123456.789);
        UseApplyPatternMethodFormat("0.00\u2030",0.789);
    }
}

结果:
123456.789 ###,###,### 123,457
123456.789 00000000.###kg 00123456.789kg
123.78 000000.000 000123.780
0.789 #.###% 78.9%
123456.789 ###.## 123456.79
0.789 0.00‰ 789.00‰
结果中可以看到以“0”特殊字符构成的模板进行格式化时,当数字某位不存在时,将显示0;
以“#”特殊字符构成的模板进行格式化操作时,格式化 后的数字位数与数字本身的位数一致
setGroupingSize()方法设置格式化数字的分组大小

import java.text.DecimalFormat;

public class DecimalMethod {
    public static void main(String[] args){
        DecimalFormat myFormat=new DecimalFormat();
        myFormat.setGroupingSize(2);
        String output=myFormat.format(123456.789);
        System.out.println("将数组以每两个数字分组"+output);
        myFormat.setGroupingUsed(false);
        String output2=myFormat.format(123456.789);
        System.out.println("不允许数字分组"+output2);
    }
}

2.数学运算
三角函数方法 在Math类中包含的三角函数方法如下。
public static double sin(double a):返回角的三角正弦。
public static double cos(double a):返回角的三角余弦。
public static double tan(double a):返回角的三角正切。
public static double asin(double a):返回一个值的反正弦。 public static double acos(double a):返回一个值的反余弦。 public static double atan(double a):返回一个值的反正切。 public static double toRadians(double angdeg):将角度转换为弧 度。
public static double toDegrees(double angrad):将弧度转换为角 度。
参数和返回值都是double

public class TrigonometricFunction{
	public static void main(String[] args){
		System.out.println("90度的正弦:"+Math.sin(Math.PI/2));
		System.out.println("0度的余弦:"+Math.cos(0));
		System.out.println("60度的正切:"+Math.tan(Math.PI/3));
		System.out.println("2的平方根与2商的反正弦值:"+Math.asin(Math.sqrt(2)/2));
		System.out.println("2的平方根与2商的反余:"+Math.acos(Math.sqrt(2)/2));
		System.out.println("1的反正切值:"+Math.sin(Math.PI/2));
		System.out.println("90度的正弦:"+Math.atan(1));
		System.out.println("120度的弧度值:"+Math.toRadians(120.0));
		System.out.println("π/2角度值:"+Math.toDegrees(Math.PI/2));
	}
}

b.指数函数方法 Math类中与指数相关的函数方法如下
public static double exp(double a):用于获取e的a次方,即取eª。
public static double log(double a):用于取自然对数,即取lna的 值。
public static double log10(double a):用于取底数为10的对数。 public static double sqrt(double a):用于取a的平方根,其中a的 值不能为负值。
public static double cbrt(double a):用于取a的立方根。
public static double pow(double a,double b):用于取a的b次方。

public class ExponentFunction {
    public static void main(String[] args) {
        System.out.println("e的平方值" +Math.exp(2));
        System.out.println("e为底2的对数值" +Math.log(2));
        System.out.println("10为底2的对数"+Math.log10(2));
        System.out.println("4平方根"+Math.exp(2));
        System.out.println("8的立方根" +Math.sqrt(2));
        System.out.println("2的2次方根" +Math.pow(2,2));
    }
}

c.取整函数
public static double ceil(double a):返回大于等于参数的最小整 数。
public static double floor(double a):返回小于等于参数的最大整 数。
public static double rint(double a):返回与参数最接近的整数, 如果两个同为整数且同样接近,则结果取偶数。
public static int round(float a):将参数加上0.5后返回与参数最 近的整数。
public static long round(double a):将参数加上0.5后返回与参数 最近的整数,然后强制转换为长整型。

3.随机数
在Math类中存在一个random()方法,用于产生随机数字。这个方法默认生成大于等于0.0且小于1.0的double型随机数,即0<=Math.random()<1.0。
Math.random()语句上稍加处理,就可以使用这个方法产生任意范围的随机数
在这里插入图片描述

public class MathRandom {
    public static int GetEvenNum(double num1,double num2){
        int s=(int)num1+(int)(Math.random()*(num2-num1));
        if(s%2==0){
            return s;
        }else
            return s+1;
    }
    public static void main(String[] args){
        System.out.println("任意一个2——32之间的偶数:"+GetEvenNum(2,32));

    }
}

使用Math类的random()方法也可以随机生成字符,可以使用如下代码生成a~z 之间的字符。
(char)(‘a’+Math.random()(‘z’-‘a’+1));
通过上述表达式可以求出更多的随机字符,如A~Z之间的随机字符。
若想生成任意两个字符之间的随机字符,可以使用以下语句实现: (char)(cha1+Math.random()
(cha2-cha1+1));

random()方法返回的值实际上是伪随机数,它通过复杂的运算而得到一系列的 数。该方法是通过当前时间作为随机数生成器的参数,所以每次执行程序都会产生 不同的随机数。

实例化一个Random对象可 以创建一个随机数生成器
语法如下:
Random r=new Random(); 其中,r是指Random对象。
语法如下: Random r=new Random(seedValue);
r:Random类对象。 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():返回一个概率密度为高斯分布的双精度值

4.大数运算
int的最大值为2^31-1,BigInteger支持任意精度的整数
语法如下:
public BigInteger(String val)
其中,val是十进制字符串
BigInteger twoInstance=new BigInteger(“2”); //将十进制2转换为 BigInteger形式
参数2的双引号不能省略,因为参数是以字符串的形式存在的。

BigInteger类中常用的几种运算方法。
public BigInteger add(BigInteger val):做加法运算。
public BigInteger subtract(BigInteger val):做减法运算。
public BigInteger multiply(BigInteger val):做乘法运算。
public BigInteger divide(BigInteger val):做除法运算。
public BigInteger remainder(BigInteger val):做取余操作。 public BigInteger[] divideAndRemainder(BigInteger val):用数 组返回余数和商,结果数组中第一个值为商,第二个值为余数。 public BigInteger pow(int exponent):进行取参数的exponent次方 操作。
public BigInteger negate():取相反数。

BigDecimal
BigDecimal类支持任何精度的定点数,可以用它来精 确计算货币值。
了BigDecimal类中实现加、减、乘、除的方法。
public BigDecimal add(BigDecimal augend):做加法操作。 public BigDecimal subtract(BigDecimal subtrahend):做减法操 作。
public BigDecimal multiply(BigDecimal multiplicand):做乘法 操作。
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):做除法操作,方法中3个参数分别代表除数、商的小数点后的位 数、近似处理模式。

BigDecimal类中divide()方法的多种处理模式
在这里插入图片描述

import java.math.BigDecimal;

public class BigDecimalDemo{
    static final int location=10;

    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 BigDecimal sub(double value1,double value2){
        BigDecimal b1=new BigDecimal(Double.toString(value1));
        BigDecimal b2=new BigDecimal(Double.toString(value2));
        return b1.subtract(b2);
    }

    public BigDecimal mul(double value1,double value2) {
        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
        return b1.multiply(b2);
    }
    public BigDecimal div(double value1,double value2){
            return div(value1,value2,location);
        }
        public BigDecimal div(double value1,double value2,int b){
            if(b<0){
                System.out.println("b必须大于等于0");
            }
            BigDecimal b1=new BigDecimal(Double.toString(value1));
            BigDecimal b2=new BigDecimal(Double.toString(value2));
            return b1.divide (b2, b, BigDecimal.ROUND_HALF_UP);
        }
        public static void main(String[] args){
            BigDecimalDemo b=new BigDecimalDemo();
            System.out.println("两个数相加结果为:"+b.add(-7.5,8.9));
            System.out.println("两个数相减结果为:"+b.sub(-7.5,8.9));
            System.out.println("两个数相乘结果为:"+b.mul(-7.5,8.9));
            System.out.println("两个数相除结果为,结果保留10位小数"+b.div(10,2));
            System.out.println("两个数相除,结果包iu5位小数结果为:"+b.div(-7.5,8.9));
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值