- 1 import java.math.BigDecimal;
- 2 import java.text.DecimalFormat;
- 3 import java.text.NumberFormat;
- 4
- 5 public class TestDecimal {
- 6 public static void main(String[] args) {
- 7 /**
- 8 * java.math.BigDecimal 该类型的数据精确度极高,适合做财务软件。
- 9 *
- 10 * 构造方法:
- 11 * BigDecimal(int) 创建一个具有参数所指定整数值的对象。
- 12 * BigDecimal(double) 创建一个具有参数所指定双精度值的对象。
- 13 * BigDecimal(long) 创建一个具有参数所指定长整数值的对象。
- 14 * BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。
- 15 *
- 16 * 方法:
- 17 * add(BigDecimal) BigDecimal对象中的值相加,然后返回这个对象。
- 18 * subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。
- 19 * multiply(BigDecimal) BigDecimal对象中的值相乘,然后返回这个对象。
- 20 * divide(BigDecimal) BigDecimal对象中的值相除,然后返回这个对象。
- 21 * toString() 将BigDecimal对象的数值转换成字符串。
- 22 * doubleValue() 将BigDecimal对象中的值以双精度数返回。
- 23 * floatValue() 将BigDecimal对象中的值以单精度数返回。
- 24 * longValue() 将BigDecimal对象中的值以长整数返回。
- 25 * intValue() 将BigDecimal对象中的值以整数返回。
- 26 */
- 27
- 28 /**
- 29 * 加法运算: 使用add()方法,不能使用加号(+)运算
- 30 */
- 31 BigDecimal v1 = new BigDecimal(10); //创建大数据对象
- 32 BigDecimal v2 = new BigDecimal(20);
- 33 //必须调用方法执行加法运算.
- 34 BigDecimal v3 = v1.add(v2);
- 35 System.out.println(v3); //30
- 36
- 37 /**
- 38 * 乘法运算: 使用multiply()方法
- 39 *
- 40 * NumberFormat:
- 41 * 由于NumberFormat类的format()方法可以使用BigDecimal对象作为其参数,
- 42 * 可以利用BigDecimal对超出16位有效数字的货币值,百分值,以及一般数值进行格式化控制。
- 43 */
- 44 NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用
- 45 NumberFormat percent = NumberFormat.getPercentInstance(); //建立百分比格式化引用
- 46 percent.setMaximumFractionDigits(3); //百分比小数点最多3位
- 47
- 48 BigDecimal loanAmount = new BigDecimal("15000.48"); //贷款金额
- 49 BigDecimal interestRate = new BigDecimal("0.008"); //利率
- 50 BigDecimal interest = loanAmount.multiply(interestRate); //相乘
- 51
- 52 System.out.println("贷款金额:\t" + currency.format(loanAmount)); // ¥15,000.48
- 53 System.out.println("利率:\t" + percent.format(interestRate)); // 0.8%
- 54 System.out.println("利息:\t" + currency.format(interest)); // ¥120.00
- 55
- 56
- 57 /**
- 58 * BigDecimal是通过compareTo() 来进行比较
- 59 */
- 60 BigDecimal a = new BigDecimal("1");
- 61 BigDecimal b = new BigDecimal("2");
- 62 BigDecimal c = new BigDecimal("1");
- 63
- 64 //左边比右边大:返回1 相等:返回0 小于:返回-1
- 65 System.out.println(a.compareTo(b)); // -1
- 66 System.out.println(a.compareTo(c)); // 0
- 67 System.out.println(b.compareTo(c)); // 1
- 68
- 69 /**
- 70 * NumberFormat:声明一个NumberFormat对象 ,得到默认的数字格式化显示
- 71 */
- 72 NumberFormat nf = NumberFormat.getInstance() ;
- 73 System.out.println("格式化之后的数字:" + nf.format(10000000)); //10,000,000
- 74 System.out.println("格式化之后的数字:" + nf.format(1000.345)); //1,000.345
- 75
- 76 /**
- 77 * DecimalFormat:
- 78 */
- 79 class FormatDemo {
- 80 public void format1(String pattern,double value){ // 此方法专门用于完成数字的格式化显示
- 81 DecimalFormat df = null; // 声明一个DecimalFormat类的对象
- 82 df = new DecimalFormat(pattern); // 实例化对象,传入模板
- 83 String str = df.format(value); // 格式化数字
- 84 System.out.println(str);
- 85 }
- 86 }
- 87 FormatDemo demo = new FormatDemo(); //格式化对象的类
- 88 demo.format1("###,###.###",111222.34567); //111,222.346
- 89 demo.format1("000,000.000",11222.34567); //011,222.346
- 90 demo.format1("###,###.###¥",111222.34567); //111,222.346¥
- 91 demo.format1("000,000.000¥",11222.34567); //011,222.346¥
- 92 demo.format1("##.###%",0.345678); //34.568%
- 93 demo.format1("00.###%",0.0345678); //03.457%
- 94 demo.format1("###.###\u2030",0.345678); //345.678‰
- 95 }
- 96 }
- 1 import java.text.DecimalFormat;
- 2
- 3 public class TestDecimal2 {
- 4 public static void main(String[] args) {
- 5 /**
- 6 * java.text.DecimalFormat
- 7 * 关于数字格式化
- 8 *
- 9 * 数字格式元素:
- 10 * # 任意数字
- 11 * , 千分位
- 12 * . 小数点
- 13 * 0 不够补0
- 14 */
- 15 //需求1:加入千分位.
- 16 DecimalFormat df = new DecimalFormat("###,###"); //创建数字格式化对象
- 17
- 18 //Number-->String
- 19 String dec = df.format(1234567); //格式化
- 20 System.out.println(dec); //"1,234,567"
- 21
- 22
- 23 //需求2:加入千分位,保留2位小数
- 24 DecimalFormat df1 = new DecimalFormat("###,###.##");
- 25 String dec1 = df1.format(1234567.123); //格式化
- 26 System.out.println(dec1); //"1,234,567.12"
- 27
- 28
- 29 //需求3:加入千分位,保留4位小数,并且不够补0
- 30 DecimalFormat df2 = new DecimalFormat("###,###.0000");
- 31 String dec2 = df2.format(1234567.123); //格式化
- 32 System.out.println(dec2);
- 33 }
- 34 }