java 来自另一个 bigDecimal.toString() 的新 BigDecimal 总是等于吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33381260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:35:51  来源:igfitidea点击:

is new a BigDecimal from another bigDecimal.toString() always equals?

java

提问by andyf

In Java, is new a BigDecimal from another bigDecimal.toString() always equals? For example

在 Java 中,来自另一个 bigDecimal.toString() 的新 BigDecimal 总是等于吗?例如

    BigDecimal a = new BigDecimal("1.23");
    BigDecimal b = new BigDecimal(a.toString());

    System.out.println(a.compareTo(b) == 0); // always true?

I know BigDecimal is immutable, but i want to know if there is any good way to clonea BigDecimal object?

我知道 BigDecimal 是不可变的,但我想知道cloneBigDecimal 对象是否有任何好方法?

回答by Sleiman Jneidi

Yes you can assume so. From the BigDecimal.toStringdocs

是的,你可以这么认为。从BigDecimal.toString文档

If that string representation is converted back to a BigDecimalusing the BigDecimal(String)constructor, then the original value will be recovered.

如果BigDecimal使用BigDecimal(String)构造函数将该字符串表示形式转换回 a ,则将恢复原始值。

However you can safely share the same object because it is immutableand copying is not required

但是您可以安全地共享同一个对象,因为它是immutable并且不需要复制

回答by user3871424

One simple way would be to add ZERO

一种简单的方法是添加零

BigDecimal a = new BigDecimal("1.23");
BigDecimal b = a.add(BigDecimal.ZERO);

回答by Greg D

Just a remark : BigDecimal is not final so it can be extended by a mutable class. Making a defensive copy can therefore be required in some use cases.

请注意:BigDecimal 不是最终的,因此可以通过可变类进行扩展。因此,在某些用例中可能需要制作防御性副本。

The BigDecimal.toString() method can be used :

可以使用 BigDecimal.toString() 方法:

 * There is a one-to-one mapping between the distinguishable
 * {@code BigDecimal} values and the result of this conversion.
 * That is, every distinguishable {@code BigDecimal} value
 * (unscaled value and scale) has a unique string representation
 * as a result of using {@code toString}.  If that string
 * representation is converted back to a {@code BigDecimal} using
 * the {@link #BigDecimal(String)} constructor, then the original
 * value will be recovered.
 * There is a one-to-one mapping between the distinguishable
 * {@code BigDecimal} values and the result of this conversion.
 * That is, every distinguishable {@code BigDecimal} value
 * (unscaled value and scale) has a unique string representation
 * as a result of using {@code toString}.  If that string
 * representation is converted back to a {@code BigDecimal} using
 * the {@link #BigDecimal(String)} constructor, then the original
 * value will be recovered.

Here an example of code making a defensive copy of BigDecimal :

这是制作 BigDecimal 防御性副本的代码示例:

public static BigDecimal safeInstance(BigDecimal val) { 
    return val.getClass() == BigDecimal.class ? 
    val : new BigDecimal(val.toString()); 
}

But does it make sense ? If a class extends the BigDecimal class you cannot be sure it does not extend its toString() method without respecting the contract...

但这有意义吗?如果一个类扩展了 BigDecimal 类,您无法确定它不会在不遵守约定的情况下扩展其 toString() 方法......

Probably it's better to invalidate any extension of BigDecimal :

可能最好使 BigDecimal 的任何扩展无效:

public static BigDecimal safeInstance(BigDecimal val) { 
    if (val.getClass() != BigDecimal.class) {
        //TODO throw exception
    }
    return val; 
}