如何應用Java的BigDecimal類

2021-06-07 13:31:53 字數 1412 閱讀 4578

//建立bigdecimal物件   

bigdecimal bignumber = new bigdecimal("89.1234567890123456789");   

bigdecimal bigrate = new bigdecimal(1000);   

bigdecimal bigresult = new bigdecimal(); //物件bigresult的值為0.0   

//對bignumber的值乘以1000,結果 賦予bigresult   

bigresult = bignumber.multiply(bigrate);   

system.out.println(bigresult.tostring());   

//或者system.out.println(bigresult);   

//顯示結果:89123.4567890123456789000   

//以雙精度數返回bignumber中的值   

double ddata = bignumber.doublevalue();   

system.out.println(ddata); //結果:89.12345678901235  

bigdecimal bigloanamount = new bigdecimal(loanamountstring);   

//建立bigdecimal物件   

bigdecimal biginterestrate = new bigdecimal(interestratestring);   

bigdecimal biginterest = bigloanamount.multiply(biginterestrate);   

//bigdecimal運算   

numberformat currency = numberformat.getcurrencyinstance();   

//建立貨幣格式化引用   

numberformat percent = numberformat.getpercentinstance();   

//建立百分比格式化引用   

percent.setmaximumfractiondigits(3); //百分比小數點最多3位   

//利用bigdecimal物件作為引數在format()中呼叫貨幣和百分比格式化   

system.out.println("loan amount:\t" + currency.format(bigloanamount));   

system.out.println("interest rate:\t" + percent.format(biginterestrate));   

system.out.println("interest:\t" + currency.format(biginterest));  

java多型應用

物件導向的三大特徵 1.封裝 2.繼承。3.多型 多型 乙個物件具備多種形態。父類的引用型別變數指向了子類的物件 或者是介面 的引用型別變數指向了介面實現類的物件 多型的前提 必須存在繼承或者實現 關係。動物 a new 狗 多型要注意 的細節 1.多型情況下,子父類存在同名的成員變數時,訪問的是父...

JAVA集合應用

public class notice list示例 公告需求管理 author ely public class noticetest 在第一條公告後面新增一調新的公告 notice notice4 newnotice 4 管理員 new date noticelist.add 1,notice4...

JAVA 多型應用

1 使用父類作為方法引數型別 2 使用父類作為方法返回值型別 注 當作為引數的父類是普通類或者抽象類時,構成繼承多型 當作為引數的父類是乙個介面時,構成介面多型 形式引數的型別 基本型別 引用型別 普通類形參 當乙個形參希望我們傳入的是乙個普通類時,我們實際上傳入的是該類的物件 匿名物件 抽象類形參...