二十二,数学运算相关的类

1. Math

数学方法类,属性和接口都是静态的,所以可以用静态导入的方式使用(static import).

示例:

package com.ares.mathdemo;
public class MathDemo {
	public static void main(String[] args) {
		System.out.println("PI = " + Math.PI);
		System.out.println(Math.max(1, 2));
		System.out.println(Math.round(89.9876));	// 四舍五入
	}
}



2. Random

取得一定范围的类的随机数.

示例:

package com.ares.randomdemo;
import java.util.Random;
public class RandomDemo {
	public static void main(String[] args) {
		Random r = new Random();
		for (int i = 0; i < 10; i++) {
			System.out.print(r.nextInt(100) + "、");
		}
	}
}



3. NumberFormat

NumberFormat也是Format类的三个子类之一,这三个子类都是和国际化编程有关的.

它有一个子类DecimalFormat,可以完成数字相关的自定义格式化操作.

示例1:

package com.ares.numberformatdemo;
import java.text.NumberFormat;
public class NumberFormatDemo {
	public static void main(String args[]) {
		int temp = 1000010000;
		NumberFormat num = NumberFormat.getInstance() ;
		System.out.println(num.format(temp));
	}
}

示例2:

package com.ares.numberformatdemo;
import java.text.DecimalFormat;
class FormatDemo {
	public void format(String pattern, double value) { // 此方法专门用于格式化
		DecimalFormat format = new DecimalFormat(pattern); // 指定操作的模板
		String str = format.format(value);// 转换操作
		System.out.println("数字按照格式(" + pattern + ")的转换结果是:" + str);
	}
}
public class DecimalFormatDemo {
	public static void main(String[] args) {
		FormatDemo format = new FormatDemo() ;
		format.format("000,000.000", 23456.45) ;
		format.format("###,###.###", 23456.45) ;
		format.format("000,000.000¥", 23456.45) ;
		format.format("###,###.###¥", 23456.45) ;
		format.format("##.###%", 0.34567) ;
		format.format("00.###%", 0.034567) ;
	}
}



4.大数操作

大树操作是指数据非常大,大到已经超过了整个数据类型的保存范围.java中为了解决这类问题,提供了两个类来完成相关的操作.

4.1 BigInteger

大整型数的操作.

示例:

package com.ares.largenumberdemo;
import java.math.BigInteger;
public class BigIntegerDemo {
	public static void main(String[] args) {
		String num1 = "9999999999999999999999999999999999";
		String num2 = "9999999999999999999999999999999998";
		BigInteger big1 = new BigInteger(num1); // 实例化BigInteger对象
		BigInteger big2 = new BigInteger(num2); // 实例化BigInteger对象
		System.out.println("加法操作:" + big1.add(big2));
		System.out.println("减法操作:" + big1.subtract(big2));
		System.out.println("乘法操作:" + big1.multiply(big2));
		System.out.println("除法操作:" + big1.divide(big2));
		BigInteger result[] = big1.divideAndRemainder(big2);// 进行触发操作,有余数
		System.out.println("相除之后的商是:" + result[0]);
		System.out.println("相除之后的余数是:" + result[1]);
	}
}



4.2 BigDecimal

处理大浮点数.

示例:

package org.lxh.largenumberdemo;
import java.math.BigDecimal;
class MyMath {
	public static double add(String num1, String num2) {
		BigDecimal bd1 = new BigDecimal(num1);
		BigDecimal bd2 = new BigDecimal(num2);
		return bd1.add(bd2).doubleValue();
	}
	public static double sub(String num1, String num2) {
		BigDecimal bd1 = new BigDecimal(num1);
		BigDecimal bd2 = new BigDecimal(num2);
		return bd1.subtract(bd2).doubleValue();
	}
	public static double mul(String num1, String num2) {
		BigDecimal bd1 = new BigDecimal(num1);
		BigDecimal bd2 = new BigDecimal(num2);
		return bd1.multiply(bd2).doubleValue();
	}
	public static double div(String num1, String num2, int scale) {
		BigDecimal bd1 = new BigDecimal(num1);
		BigDecimal bd2 = new BigDecimal(num2);
		return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	public static double round(double num, int scale) {
		BigDecimal bd1 = new BigDecimal(num);
		BigDecimal bd2 = new BigDecimal(1);
		return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}
public class BigDecimalDemo {
	public static void main(String[] args) {
		String num1 = "12345.07891";
		String num2 = "3333.5101";
		System.out.println("加法操作:" + MyMath.round(MyMath.add(num1, num2), 2));
		System.out.println("减法操作:" + MyMath.round(MyMath.sub(num1, num2), 2));
		System.out.println("乘法操作:" + MyMath.round(MyMath.mul(num1, num2), 2));
		System.out.println("除法操作:" + (MyMath.div(num1, num2, 2)));
	}
}







20150428


JAVA学习笔记系列

--------------------------------------------

                    联系方式

--------------------------------------------

        Weibo: ARESXIONG

        E-Mail: [email protected]

------------------------------------------------

你可能感兴趣的:(Random类,NumberFormat类,Math类,大数操作)