数学相关类型

   a.Math类
     java.lang.Math类提供常用数学功能和数学常量
   b.Random类
     java.util.Random类是基于"线性同余"算法的一种伪随机数序列生成器。
   c.BigInteger/BigDecimal类
     java.math包中定义的BigInteger和BigDecimal类型可以分别提供任意长度/精度的整数和浮点数运算功能
   d.NumberFormat/DecimalFormat类(DecimalFormat是NumberFormat的子类)
     java.text.NumberFormat/DecimalFormat提供了将数字格式化为语言环境相关字符串以及逆向解析字符串

     为数字的功能

import java.text.DecimalFormat;

//对有小数的数进行操作的举例,也应该好好研究掌握!!!
public class TestDecimal {
	public static void main(String[] args) {
		double d = 12345.67;
		DecimalFormat df1 = new DecimalFormat();
		DecimalFormat df2 = new DecimalFormat("###,###.0000");
		DecimalFormat df3 = new DecimalFormat("000,000.####");
		System.out.println(df1.format(d));
		System.out.println(df2.format(d));
		System.out.println(df3.format(d));

		// 将给定的模式应用于此 Format 对象。
		df3.applyPattern("0.0000E00");
		System.out.println(df3.format(d));
		df3.applyPattern("\u00A4###,###.00%");
		System.out.println(df3.format(d));
		df3.applyPattern("00.00%");
		System.out.println(df3.format(0.34819));

		try {
			double d1 = df1.parse("35.2974").doubleValue();
			double d2 = df2.parse("1,4135.2974").doubleValue();
			double d3 = df3.parse("92.37%").doubleValue();
			System.out.println(d1);
			System.out.println(d2);
			System.out.println(d3);
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		// 带有四舍五入的功能(0-5舍,6-9入)......掌握!!!
		DecimalFormat df = new DecimalFormat(".00");
		String value = df.format(12334.126);
		System.out.println("经常使用的格式化" + value);
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值