我是靠谱客的博主 魔幻小虾米,最近开发中收集的这篇文章主要介绍java bigdecimal取整数,如何在Java中获取BigDecimal的整数和分数部分,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

For a BigDecimal number such as 42.7, how can I tear this apart to get the integer part (42) and the decimal fractional part (0.7)?

I would like to retrieve both as BigDecimal objects. But I will take what I can get and make BigDecimal object from them.

解决方案

tl;dr

myBigDecimal

.divideAndRemainder( BigDecimal.ONE )

[ 0 ] // for integer part

…and…

[ 1 ] // for fraction part

BigDecimal::divideAndRemainder

The BigDecimal class offers some methods for this.

The BigDecimal::divideAndRemainder method divides your original number by another number. The result is an array of two BigDecimal objects, the first being the integer portion and the second being the fraction amount.

To achieve your goal, we merely need to divide by one. The BigDecimal class offers a constant object for the value of one: BigDecimal.ON

最后

以上就是魔幻小虾米为你收集整理的java bigdecimal取整数,如何在Java中获取BigDecimal的整数和分数部分的全部内容,希望文章能够帮你解决java bigdecimal取整数,如何在Java中获取BigDecimal的整数和分数部分所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部