BigDecimal(String)鏄妸涓涓互String琛ㄧず鐨凚igDecimal瀵硅薄鏋勯犱負BigDecimal瀵硅薄瀹炰緥銆?/p>
涔犳儻涓婏紝瀵逛簬嫻偣鏁版垜浠兘浼氬畾涔変負double鎴杅loat錛屼絾BigDecimal API鏂囨。涓浜嶣igDecimal(double)鏈夎繖涔堜竴孌佃瘽錛?/p>
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 .10000000000000000555111512312578 27021181583404541015625. 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 notwithstanding.
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
涓嬮潰瀵硅繖孌佃瘽鍋氱畝鍗曡В閲婏細
娉ㄦ剰錛氳繖涓瀯閫犲櫒鐨勭粨鏋滃彲鑳戒細鏈変笉鍙鐭ョ殑緇撴灉銆傛湁浜哄彲鑳借鎯?/span>new BigDecimal(.1)絳変簬.1鏄紜殑錛屼絾瀹冨疄闄呬笂鏄瓑浜?/span>.1000000000000000055511151231257827021181583404541015625錛岃繖灝辨槸涓轟粈涔?/span>.1涓嶈兘鐢ㄤ竴涓?/span>double綺劇‘琛ㄧず鐨勫師鍥狅紝鍥犳錛岃繖涓鏀捐繘鏋勯犲櫒涓殑闀垮煎茍涓嶇簿紜殑絳変簬.1錛屽敖綆″瑙傜湅璧鋒潵鏄浉絳夌殑銆?/span>
鐒惰岋紙String錛夋瀯閫犲櫒錛屽垯瀹屽叏鍙鐭ョ殑錛?/span>new BigDecimal(“.1”)濡傚悓鏈熸湜鐨勯偅鏍風簿紜殑絳変簬.1錛屽洜姝わ紝錛?/span>String錛夋瀯閫犲櫒鏄浼樺厛鎺ㄨ崘浣跨敤鐨勩?/span>
鐪嬩笅闈㈢殑緇撴灉錛?/p>
System.out.println(new BigDecimal(123456789.02).toString());
System.out.println(new BigDecimal("123456789.02").toString());
杈撳嚭涓猴細
123456789.01999999582767486572265625
123456789.02
鐜板湪鎴戜滑鐭ラ亾錛屽鏋滈渶瑕佺簿紜綆楋紝闈炶鐢⊿tring鏉ュ閫燘igDecimal涓嶅彲錛?/p>