BigDecimal - 使用new或valueOf [英] BigDecimal - to use new or valueOf

查看:1842
本文介绍了BigDecimal - 使用new或valueOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了两种方法,即从双d中获取BigDecimal对象。

I came across two ways of getting BigDecimal object out of a double d.

1. new BigDecimal(d)
2. BigDecimal.valueOf(d)

这将是一个更好的方法?
valueOf会创建一个新对象吗?

Which would be a better approach? Would valueOf create a new object?

一般情况下(不只是BigDecimal),推荐什么 - new或valueOf?

In general (not just BigDecimal), what is recommended - new or valueOf?

谢谢。

推荐答案

这是两个独立的问题:我应该为<$ c $使用什么? C>的BigDecimal ?和我一般做什么?

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

对于 BigDecimal :这有点棘手,因为他们<强>不要做同样的事情。 BigDecimal .valueOf(double) 将使用 规范字符串表示 double 值以实例化 BigDecimal 对象。换句话说:当您执行 System.out.println(d) BigDecimal 对象的值将是您所看到的值。 c>。

For BigDecimal: this is a bit tricky, because they don't do the same thing. BigDecimal.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).

如果您使用 new BigDecimal(d) 但是, BigDecimal 将尝试尽可能准确地表示 double 。这将通常导致存储的数字比你想要的多得多。严格来说,它比 valueOf()更正确,但它更不直观。

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.

有一个很好的解释在JavaDoc中的这个:

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


这个构造函数的结果可能有点不可预测。有人可能会认为在Java中编写 new BigDecimal(0.1)会创建一个 BigDecimal ,它恰好等于0.1(未缩放值为1,范围为1),但实际上等于0.1000000000000000055511151231257827021181583404541015625。这是因为0.1不能完全表示为 double (或者,就此而言,作为任何有限长度的二进制分数)。因此,传递给构造函数的值并不完全等于0.1,尽管有外观。

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.

一般情况下,如果结果是相同的(即不是 BigDecimal ,但在大多数情况下),那么 valueOf() 应该是首选的:它可以缓存常见值(如 Integer.valueOf() )它甚至可以改变缓存行为,而无需调用者要改变。 new 总是实例化一个新值,即使没有必要(最好的例子: new Boolean(true) vs. Boolean.valueOf(true))。

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)).

这篇关于BigDecimal - 使用new或valueOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆