# Java中支持链式调用的数字工具类

{% embed url="<https://v.douyin.com/r81XBGX/>" %}

{% embed url="<https://www.bilibili.com/video/BV1v54y1o7a5/>" %}

我们都知道

java里边

可以用这个 BigDecimal 来解决

小数点运算的精度问题

比方说这个0.3加0.6就会出现这种情况

那

我之前一直用的都是 HuTool 的这个 NumberUtil

来解决

因为它也是封装了这个 BigDecimal

但我发现如果这个表达是比较长

同时呢他中间的符号又不一样

比如说 a 加 b 减 c

这样的话呢

我们就需要先把这个 a 加 b 算出来

然后再套一层

那三个还好

如果再多的话呢

这个代码可读性就不是特别好

所以呢

我就想到简单封装一个支持

链式调用 的\~

这么一个工具类

比如说这样 startOf

然后呢 add(b)

然后呢 subtract(c)

这样就可以了

最终我们可以.getDouble

然后呢还可以传一个精度

比如说四舍五入两位小数

这样的话他就拿到一个结果

那这个类呢也很简单

但如果不想自己写的话呢

我把它也传到了 GitHub 上


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sunzsh.gitbook.io/xiaoshan.bug/java-zhong-zhi-chi-lian-shi-diao-yong-de-shu-zi-gong-ju-lei.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
