BigDecimal
鍦ㄣ奅ffective Java銆嬭繖鏈功涓篃鎻愬埌榪欎釜鍘熷垯錛宖loat鍜宒ouble鍙兘鐢ㄦ潵鍋氱瀛﹁綆楁垨鑰呮槸宸ョ▼璁$畻錛屽湪鍟嗕笟璁$畻涓垜浠鐢╦ava.math.BigDecimal銆侭igDecimal涓鍏辨湁4涓閫犳柟娉曪紝鎴戜滑涓嶅叧蹇冪敤BigInteger鏉ュ閫犵殑閭d袱涓紝閭d箞榪樻湁涓や釜錛屽畠浠槸錛?br />
BigDecimal(double val)
Translates a double into a BigDecimal.
BigDecimal(String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
涓婇潰鐨凙PI綆瑕佹弿榪扮浉褰撶殑鏄庣‘錛岃屼笖閫氬父鎯呭喌涓嬶紝涓婇潰鐨勯偅涓涓嬌鐢ㄨ搗鏉ヨ鏂逛究涓浜涖傛垜浠彲鑳芥兂閮戒笉鎯沖氨鐢ㄤ笂浜嗭紝浼氭湁浠涔堥棶棰樺憿錛熺瓑鍒板嚭浜嗛棶棰樼殑鏃跺欙紝鎵嶅彂鐜頒笂闈㈠摢涓閫犳柟娉曠殑璇︾粏璇存槑涓湁榪欎箞涓孌碉細
Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
鍘熸潵鎴戜滑濡傛灉闇瑕佺簿紜綆楋紝闈炶鐢⊿tring鏉ュ閫燘igDecimal涓嶅彲錛佸湪銆奅ffective Java銆嬩竴涔︿腑鐨勪緥瀛愭槸鐢⊿tring鏉ュ閫燘igDecimal鐨勶紝浣嗘槸涔︿笂鍗存病鏈夊己璋冭繖涓鐐癸紝榪欎篃璁告槸涓涓皬灝忕殑澶辮鍚с?br />
瑙e喅鏂規
鐜板湪鎴戜滑宸茬粡鍙互瑙e喅榪欎釜闂浜嗭紝鍘熷垯鏄嬌鐢˙igDecimal騫朵笖涓瀹氳鐢⊿tring鏉ュ閫犮?br />
浣嗘槸鎯沖儚涓涓嬪惂錛屽鏋滄垜浠鍋氫竴涓姞娉曡繍綆楋紝闇瑕佸厛灝嗕袱涓誕鐐規暟杞負String錛岀劧鍚庡閫犳垚BigDecimal錛屽湪鍏朵腑涓涓笂璋冪敤add鏂規硶錛屼紶鍏ュ彟涓涓綔涓哄弬鏁幫紝鐒跺悗鎶婅繍綆楃殑緇撴灉錛圔igDecimal錛夊啀杞崲涓烘誕鐐規暟銆備綘鑳藉蹇嶅彈榪欎箞鐑︾悙鐨勮繃紼嬪悧錛熶笅闈㈡垜浠彁渚涗竴涓伐鍏風被Arith鏉ョ畝鍖栨搷浣溿傚畠鎻愪緵浠ヤ笅闈欐佹柟娉曪紝鍖呮嫭鍔犲噺涔橀櫎鍜屽洓鑸嶄簲鍏ワ細
public static double add(double v1,double v2)
public static double sub(double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)
闄勫綍
婧愭枃浠禔rith.java錛?br />
import java.math.BigDecimal;
/**
* 鐢變簬Java鐨勭畝鍗曠被鍨嬩笉鑳藉綺劇‘鐨勫嫻偣鏁拌繘琛岃繍綆楋紝榪欎釜宸ュ叿綾繪彁渚涚簿
* 紜殑嫻偣鏁拌繍綆楋紝鍖呮嫭鍔犲噺涔橀櫎鍜屽洓鑸嶄簲鍏ャ?br />
*/
public class Arith{
//榛樿闄ゆ硶榪愮畻綺懼害
private static final int DEF_DIV_SCALE = 10;
//榪欎釜綾諱笉鑳藉疄渚嬪寲
private Arith(){
}
/**
* 鎻愪緵錛堢浉瀵癸級綺劇‘鐨勯櫎娉曡繍綆椼傚綋鍙戠敓闄や笉灝界殑鎯呭喌鏃訛紝鐢眘cale鍙傛暟鎸?br />
* 瀹氱簿搴︼紝浠ュ悗鐨勬暟瀛楀洓鑸嶄簲鍏ャ?br />
* @param v1 琚櫎鏁?br />
* @param v2 闄ゆ暟
* @param scale 琛ㄧず琛ㄧず闇瑕佺簿紜埌灝忔暟鐐逛互鍚庡嚑浣嶃?br />
* @return 涓や釜鍙傛暟鐨勫晢
*/
public static double div(double v1,double v2,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 鎻愪緵綺劇‘鐨勫皬鏁頒綅鍥涜垗浜斿叆澶勭悊銆?br />
* @param v 闇瑕佸洓鑸嶄簲鍏ョ殑鏁板瓧
* @param scale 灝忔暟鐐瑰悗淇濈暀鍑犱綅
* @return 鍥涜垗浜斿叆鍚庣殑緇撴灉
*/
public static double round(double v,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
};