Java,仅提取BigDecimal的小数部分? [英] Java, extract just the fractional part of a BigDecimal?
本文介绍了Java,仅提取BigDecimal的小数部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,我正在使用BigDecimal
类,部分代码要求我从其中提取小数部分. BigDecimal
似乎没有任何内置方法来帮助我获取BigDecimal
小数点后的数字.
In Java, I'm working with the BigDecimal
class and part of my code requires me to extract the fractional part from it. BigDecimal
does not appear to have any built in methods to help me get the number after the decimal point of a BigDecimal
.
例如:
BigDecimal bd = new BigDecimal("23452.4523434");
我想从上面表示的数字中提取4523434
.最好的方法是什么?
I want to extract the 4523434
from the number represented above. What's the best way to do it?
推荐答案
我会尝试bd.remainder(BigDecimal.ONE)
.
Uses the remainder
method and the ONE
constant.
BigDecimal bd = new BigDecimal( "23452.4523434" );
BigDecimal fractionalPart = bd.remainder( BigDecimal.ONE ); // Result: 0.4523434
这篇关于Java,仅提取BigDecimal的小数部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文