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提供了将数字格式化为语言环境相关字符串以及逆向解析字符串
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);
}
}