计算bigdecimals的百分比 [英] Compute percentage for bigdecimals

查看:1788
本文介绍了计算bigdecimals的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到任何本地方法来执行此操作,因此我在帮助程序类中创建了自己的方法:

I haven't found any native method to do this, so I created my own in a helper class:

public static BigDecimal percentage(BigDecimal base, BigDecimal pct){
    return base.multiply(pct).divide(new BigDecimal(100));
}

但我不太喜欢它,我想知道API是否有东西类似。 Number类(BigDecimal的祖先)将是一个不错的地方。

But I don't quite like it, I wonder if the API has something similar. The Number class (ancestor of BigDecimal) would be a nice place.

推荐答案

随意子类 BigDecimal 并添加该方法。除此之外,你期待什么?您知道在哪里可以找到API并确认您希望使用该方法的类不会。就我个人而言,我认为功能是如此微不足道,以至于在标准API中没有太多意义。

Feel free to subclass BigDecimal and add that method. Beyond that, what do you expect? You know where to find the API and confirm that the class you would like to have that method doesn't. Personally, I'd say the functionality is so trivial that there wouldn't be much of a point in having it in the standard API.

这篇关于计算bigdecimals的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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