bigdecimal 去除多余的0
时间: 2024-04-28 11:25:05 浏览: 86
BigDecimal
5星 · 资源好评率100%
在使用BigDecimal时,如果想要去除多余的0,可以使用BigDecimal的"stripTrailingZeros()"方法。该方法可以去除小数点后多余的0。不过需要注意的是,使用"stripTrailingZeros()"方法后,如果直接调用toString()方法可能会得到科学计数法的结果。解决这个问题的方法是将"toString()"方法改为"toPlainString()"方法,这样就可以得到去除多余0的结果。例如:
BigDecimal bigDecimal1 = new BigDecimal("12.00");
BigDecimal bigDecimal2 = new BigDecimal("6");
BigDecimal result = bigDecimal2.divide(bigDecimal1, 4, BigDecimal.ROUND_HALF_UP);
System.out.println(result.stripTrailingZeros()); // 0.5
BigDecimal bigDecimal3 = new BigDecimal("12.00");
BigDecimal bigDecimal4 = new BigDecimal("1200.00");
BigDecimal result2 = bigDecimal4.divide(bigDecimal3, 4, BigDecimal.ROUND_HALF_UP);
System.out.println(result2.stripTrailingZeros().toPlainString()); // 100
这样就可以得到去除多余的0的结果了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [如何去除BigDecimal小数点后多余的0](https://blog.csdn.net/XXX1238XGH/article/details/126626248)[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: 50%"]
- *3* [SQLServer多种去除尾数多余的0](https://download.csdn.net/download/qq_26924717/11146144)[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: 50%"]
[ .reference_list ]
阅读全文