BigDecimal的补充 [英] Addition for BigDecimal

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

问题描述

我想用一些以 BigDecimal 类型表示的货币值做一些简单的总结。

I want to do some simple sums with some currency values expressed in BigDecimal type.

BigDecimal test = new BigDecimal(0);
System.out.println(test);
test.add(new BigDecimal(30));
System.out.println(test);
test.add(new BigDecimal(45));
System.out.println(test);

显然我不太了解 BigDecimal 算术,见后面的输出。

Obviously I do not understand well the BigDecimal arithmetics, see output behind.

Test
0
0
0

任何人都可以帮助我吗?

Can anyone help me out?

推荐答案

BigDecimal 是不可变的,所以你需要这样做:

The BigDecimal is immutable so you need to do this:

BigDecimal result = test.add(new BigDecimal(30));
System.out.println(result);

这篇关于BigDecimal的补充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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