Java,仅提取BigDecimal的小数部分? [英] Java, extract just the fractional part of a BigDecimal?

查看:3236
本文介绍了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).

使用 remainder 方法和 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屋!

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