java – 如何添加到BigDecimal
发布时间:2020-12-14 17:41:14 所属栏目:Java 来源:网络整理
导读:我有以下函数,它迭代一个数组,在每个’Refund’对象上调用一个方法,该方法返回一个包含一些值的BigDecimal,例如20.45: private String getTransactionTotals(Refund[] refunds) { BigDecimal total = new BigDecimal(0.00); /* * Itterates over all the re
我有以下函数,它迭代一个数组,在每个’Refund’对象上调用一个方法,该方法返回一个包含一些值的BigDecimal,例如20.45:
private String getTransactionTotals(Refund[] refunds) { BigDecimal total = new BigDecimal(0.00); /* * Itterates over all the refund objects and adds * their amount payables together to get a total */ for ( Refund refund : refunds ) { total.add(refund.getAmountPayable()); } total = total.setScale(2,RoundingMode.CEILING); return total.toString(); } 问题是它总是返回’0.00′.我知道我传递的数组不是null,而且’getAmountPayable()’函数返回的值也不是null或者等于0.我不知道我是不是一直盯着这太长了,我错过了明显的,一些帮助将非常感激. PS – ‘getAmountPayble()’返回BigDecimal类型的值 解决方法
您需要使用add的返回值,因为BigDecimal是不可变的.所以你要:
total = total.add(refund.getAmountPayable()); (就我个人而言,如果该方法被称为加号而不是添加,那么这将更加明显.但是没关系.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |