两个int相除并保留2位小数
时间: 2024-05-18 18:16:26 浏览: 117
要计算两个int相除并保留2位小数,可以使用BigDecimal类来实现。首先将两个int转换为double类型,然后使用BigDecimal的divide方法进行除法运算,并设置保留小数位数为2。最后使用setScale方法将结果保留2位小数并转换为double类型。
代码示例:
```java
int num1 = 6;
int num2 = 9;
BigDecimal result = BigDecimal.valueOf((double) num1 / num2).setScale(2, RoundingMode.HALF_UP);
double resultDouble = result.doubleValue();
```
其中,num1和num2分别代表两个int,result是BigDecimal类型的结果,resultDouble是最终保留2位小数的double类型结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [java的用来处理数字方面的逻辑工具类](https://download.csdn.net/download/songylwq/5538765)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [两个int相除并保留两位小数](https://blog.csdn.net/m0_61494411/article/details/129157101)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [C语言通用范例开发金典.part2.rar](https://download.csdn.net/download/xqq524148626/4539171)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文