﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-轻帆向南-随笔分类-oj</title><link>http://www.blogjava.net/dongond/category/50864.html</link><description>大学记忆，过往的终点，新的起点...</description><language>zh-cn</language><lastBuildDate>Wed, 15 Feb 2012 08:50:34 GMT</lastBuildDate><pubDate>Wed, 15 Feb 2012 08:50:34 GMT</pubDate><ttl>60</ttl><item><title>about BigDecimal</title><link>http://www.blogjava.net/dongond/archive/2012/02/15/370036.html</link><dc:creator>轻帆向南</dc:creator><author>轻帆向南</author><pubDate>Wed, 15 Feb 2012 08:50:00 GMT</pubDate><guid>http://www.blogjava.net/dongond/archive/2012/02/15/370036.html</guid><wfw:comment>http://www.blogjava.net/dongond/comments/370036.html</wfw:comment><comments>http://www.blogjava.net/dongond/archive/2012/02/15/370036.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dongond/comments/commentRss/370036.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dongond/services/trackbacks/370036.html</trackback:ping><description><![CDATA[在java中做大数运算时要用到BigDecimal类。<br />何谓大数？<br />&nbsp;&nbsp;&nbsp;&nbsp;单精度浮点数：float--32位（4字节）--有效数字7位；<br />&nbsp;&nbsp;&nbsp;&nbsp;双精度浮点数：double--64位（8字节）--有效数字16位；<br />&nbsp;&nbsp;&nbsp;&nbsp;超过double表示范围的，一律用BigDecimal。<br />关于BigDecimal的构造，需要从String构造，切记不可由double构造<br />&nbsp; &nbsp; 即不可用 new BigDecimal(double var) [X]<br />&nbsp; &nbsp; 而是通过 new BigDecimal(String var)<br />&nbsp; &nbsp; 原因参考<a href="http://hi.baidu.com/waiting__for__you/blog/item/967206ec863751d3b21cb170.html">http://hi.baidu.com/waiting__for__you/blog/item/967206ec863751d3b21cb170.html<br /></a>【<br />&nbsp; &nbsp;&nbsp;<span style="color: #88b03f; font-family: Arial; font-size: 12px; line-height: normal; text-indent: 29px; background-color: #e6e9d8; ">BigDecimal(double)是把一个double类型十进制数构造为一个BigDecimal对象实例。</span><div><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; ">BigDecimal(String)是把一个以String表示的BigDecimal对象构造为BigDecimal对象实例。</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; ">习惯上，对于浮点数我们都会定义为double或float，但BigDecimal API文档中对于BigDecimal(double)有这么一段话：</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; "><span style="font-size: 9pt; color: red; ">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.</span></p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; "><span style="font-size: 9pt; color: red; ">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></p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; "></p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; "></p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; "><span style="font-size: 9pt; ">下面对这段话做简单解释：</span></p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; "><strong><span style="font-size: 9pt; ">注意：这个构造器的结果可能会有不可预知的结果。有人可能设想</span></strong><strong><span style="font-size: 9pt; ">new BigDecimal(.1)</span></strong><strong><span style="font-size: 9pt; ">等于</span></strong><strong><span style="font-size: 9pt; ">.1</span></strong><strong><span style="font-size: 9pt; ">是正确的，但它实际上是等于</span></strong><strong><span style="font-size: 9pt; ">.1000000000000000055511151231257827021181583404541015625</span></strong><strong><span style="font-size: 9pt; ">，这就是为什么</span></strong><strong><span style="font-size: 9pt; ">.1</span></strong><strong><span style="font-size: 9pt; ">不能用一个</span></strong><strong><span style="font-size: 9pt; ">double</span></strong><strong><span style="font-size: 9pt; ">精确表示的原因，因此，这个被放进构造器中的长值并不精确的等于</span></strong><strong><span style="font-size: 9pt; ">.1</span></strong><strong><span style="font-size: 9pt; ">，尽管外观看起来是相等的。</span></strong></p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; "><strong><span style="font-size: 9pt; ">然而（</span></strong><strong><span style="font-size: 9pt; ">String</span></strong><strong><span style="font-size: 9pt; ">）构造器，则完全可预知的，</span></strong><strong><span style="font-size: 9pt; ">new BigDecimal(&#8220;.1&#8221;)</span></strong><strong><span style="font-size: 9pt; ">如同期望的那样精确的等于</span></strong><strong><span style="font-size: 9pt; ">.1</span></strong><strong><span style="font-size: 9pt; ">，因此，（</span></strong><strong><span style="font-size: 9pt; ">String</span></strong><strong><span style="font-size: 9pt; ">）构造器是被优先推荐使用的。</span></strong></p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; ">看下面的结果：</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(new BigDecimal(123456789.02).toString());</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(new BigDecimal("123456789.02").toString());</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; ">输出为：</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 21pt; text-indent: 21.75pt; ">123456789.01999999582767486572265625</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 21pt; text-indent: 21.75pt; ">123456789.02</p><p style="line-height: normal; color: #88b03f; font-family: Arial; font-size: 12px; background-color: #e6e9d8; margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21.75pt; ">现在我们知道，如果需要精确计算，非要用String来够造BigDecimal不可！</p></div>】<br /><br /><br /><br />&nbsp;<br /><br /><img src ="http://www.blogjava.net/dongond/aggbug/370036.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dongond/" target="_blank">轻帆向南</a> 2012-02-15 16:50 <a href="http://www.blogjava.net/dongond/archive/2012/02/15/370036.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>