我是靠谱客的博主 斯文楼房,最近开发中收集的这篇文章主要介绍BigDecimal.valueOf,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Those are two separate questions: "What should I use for BigDecimal?" and "What do I do in general?"

For BigDecimal: this is a bit tricky, because they don't do the same thingBigDecimal.valueOf(double) will use the canonical String representation of the double value passed in to instantiate the BigDecimal object. In other words: The value of the BigDecimal object will be what you see when you do System.out.println(d).

If you use new BigDecimal(d) however, then the BigDecimal will try to represent the double value as accurately as possible. This will usually result in a lot more digits being stored than you want. Strictly speaking, it's more correct than valueOf(), but it's a lot less intuitive.

There's a nice explanation of this in the JavaDoc:

The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

In general, if the result is the same (i.e. not in the case of BigDecimal, but in most other cases), then valueOf() should be preferred: it can do caching of common values (as seen on Integer.valueOf()) and it can even change the caching behaviour without the caller having to be changed. new will always instantiate a new value, even if not necessary (best example: new Boolean(true) vs. Boolean.valueOf(true)).

share improve this answer
  • 3
    Awesome answer (+1) – Sean Patrick Floyd Aug 25 '11 at 7:45
  •  
    This also explains my question: stackoverflow.com/questions/15685705/… – Christian Mar 28 '13 at 17:22
  • 1
    @Joachim, it wasn't clear. Is new BigDecimal() better than BigDecimal.valueOf()? – ryvantage Dec 24 '13 at 22:20
  • 4
    @ryvantage: if one where strictly better than the other then there would be no need for both and my answer would be much shorter. They don't do the same thing, so you can't rank them at like that. – Joachim Sauer Dec 24 '13 at 22:27
  • 2
    @JoachimSauer, ok sorry I should've been more specific. Would you mind giving an example of when new BigDecimal() would be preferred and an example of when BigDecimal.valueOf() would be preferred? – ryvantage Dec 24 '13 at 22:43

39

If you are using your BigDecimal objects to store currency values, then I strongly recommend that you do NOT involve any double values anywhere in their calculations.

As stated in another answer, there are known accuracy issues with double values and these will come back to haunt you big time.

Once you get past that, the answer to your question is simple. Always use the constructor method with the String value as the argument to the constructor, as there is no valueOf method for String.

If you want proof, try the following:

BigDecimal bd1 = new BigDecimal(0.01); BigDecimal bd2 = new BigDecimal("0.01"); System.out.println("bd1 = " + bd1); System.out.println("bd2 = " + bd2);

You'll get the following output:

bd1 = 0.01000000000000000020816681711721685132943093776702880859375 bd2 = 0.01

转载于:https://www.cnblogs.com/kakaisgood/p/10751489.html

最后

以上就是斯文楼房为你收集整理的BigDecimal.valueOf的全部内容,希望文章能够帮你解决BigDecimal.valueOf所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部