java BigDecimal 赋值运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1859751/
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-10-29 18:16:29  来源:igfitidea点击:

BigDecimal assign operator

javabigdecimal

提问by soField

I have a problem with assigning one big decimal value to another

我在将一个大十进制值分配给另一个时遇到问题

I am trying such as creating one temp big decimal and add 0 to another big decimal

我正在尝试创建一个临时大十进制并将 0 添加到另一个大十进制

BigDecimal temp = new BigDecimal(0);
dropStartValue =  temp.add(newCounterValue);

However, I only want simply do the operation below on big decimals:

但是,我只想简单地对大数执行以下操作:

dropStartValue = newCounterValue

回答by Jon Skeet

You haven't specified the type of either dropStartValueor newCounterValue. If they're both BigDecimals, then this should be fine:

您尚未指定dropStartValue或的类型newCounterValue。如果它们都是 BigDecimals,那么这应该没问题:

dropStartValue = newCounterValue;

Note that although that's just making both variables refer to the same object, it's safe because BigDecimalitself is immutable.

请注意,虽然这只是让两个变量都指向同一个对象,但它是安全的,因为BigDecimal它本身是不可变的。

If that's not working for you, please give details of what problems you're seeing (exceptions? compile-time errors?).

如果这对您不起作用,请详细说明您看到的问题(例外?编译时错误?)。

回答by Romain

Assuming this is Java ans newCounterValue is an integer type or a box thereof, dropStartValue = new BigDecimal(newCounterValue);should do what you want.

假设这是 Java ans newCounterValue 是整数类型或其框,dropStartValue = new BigDecimal(newCounterValue);应该做你想做的。