Java 的 BigDecimal.power(BigDecimal exponent):是否有 Java 库可以做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16441769/
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
Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?
提问by Geoffrey De Smet
Java's BigDecimal.pow(int)
method only accepts an integer parameter, no BigDecimal
parameter.
Java的BigDecimal.pow(int)
方法只接受一个整型参数,没有BigDecimal
参数。
Is there a library, like Apache's commons-lang, that supports BigDecimal.pow(BigDecimal)
? It should be able to do calculate "1.21".pow("0.5")
to return "1.1"
.
是否有像 Apache 的 commons-lang 这样的库支持BigDecimal.pow(BigDecimal)
?应该可以做计算"1.21".pow("0.5")
返回"1.1"
。
采纳答案by higuaro
There is a Math.BigDecimal implementation of core mathematical functionswith source code available from the Cornell University Library here(also you can download the library as a tar.gz). Here is a sample of the library use:
有一个Math.BigDecimal实现的核心数学函数与来自康奈尔大学图书馆可用的源代码在这里(你也可以下载库作为tar.gz的)。这是库使用的示例:
import org.nevec.rjm.*;
import java.math.BigDecimal;
public class test {
public static void main(String... args) {
BigDecimal a = new BigDecimal("1.21");
BigDecimal b = new BigDecimal("0.5");
System.out.println(BigDecimalMath.pow(a, b).toString());
}
}
Prints out:
打印出来:
1.1
Update
更新
The license information is now clear in the May 2015 update:
在 2015 年 5 月的更新中,许可证信息现在很清楚:
The full source code is made available under the LGPL v3.0.
完整的源代码在 LGPL v3.0 下可用。
回答by rajesh
Havent used, but saw suanshu. An open source version in github (https://github.com/nmdev2020/SuanShu).
没用过,看过酸书。github 中的开源版本 ( https://github.com/nmdev2020/SuanShu)。
BigDecimalUtils has a pow() which suits your needs
BigDecimalUtils 有一个适合您需求的 pow()
public static java.math.BigDecimal pow(java.math.BigDecimal a,
java.math.BigDecimal b)
Compute a to the power of b.