作者ogamenewbie (._.)
看板java
标题Re: [J2SE] 进行浮点数运算时如何四舍五入至小数第 …
时间Tue Oct 9 17:41:18 2007
※ 引述《thinkniht ()》之铭言:
: 有些事情想确认一下
: 因为我很少用四舍五入的东西
: 因为numbrformat有点看不懂...
: 我有用DecimalFormat去做
: 1.觉得用比较简单
: 2.我想效果是一样的
: 结果像2.25...
: 正常四舍五入应该是2.3
: 我的结果是2.2
: 对2.375四舍五入的结果是2.4
: 根据o大的讲解...
: 这也都是因为浮点数的误差关系吗
: 另外这麽说来的话
: 如果想要进行正确的四舍五入运算
: 是否应该要把double转成字串
: 然後再使用BigDecimal
: 虽然a大说的我看不太懂...
: 得再去google研究一下相关参数之类的
: 不过之前是因为连要找啥都没啥头绪
: 很感谢大大们给了我该从哪着手的方向=.=+
关於 double 转 BigDecimal 的精确..
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html
#BigDecimal(double)
==========================================================================
public BigDecimal(double val)
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.
==========================================================================
关於 DecimalFormat 的四舍五入
http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
==========================================================================
Rounding
DecimalFormat uses half-even rounding (see ROUND_HALF_EVEN) for formatting.
==========================================================================
ROUND_HALF_EVEN
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html
#ROUND_HALF_EVEN
==========================================================================
Rounding mode to round towards the "nearest neighbor"
unless
both neighbors are equidistant,
in which case, round towards the even neighbor.
Behaves as for ROUND_HALF_UP if the digit to
the left of the discarded fraction is odd;
behaves as for ROUND_HALF_DOWN if it's even.
Note that this is the rounding mode that minimizes cumulative error
when applied repeatedly over a sequence of calculations.
==========================================================================
如果你是用 Java SE 6
DecimalFormat 好像可以换四舍五入的方法
http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html
==========================================================================
DecimalFormat provides rounding modes defined in RoundingMode for formatting.
By default, it uses RoundingMode.HALF_EVEN.
==========================================================================
--
果然还是翻 Docs 最快了.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 163.25.148.49
※ 编辑: ogamenewbie 来自: 163.25.148.49 (10/09 17:47)
※ 编辑: ogamenewbie 来自: 163.25.148.49 (10/09 17:48)
1F:推 archerlin:所以说一开始我就丢这页啦= = 後面我的举例也只是方法一 10/09 21:40