在Java中如何获取BigDecimal的整数部分和小数部分

huangapple go评论68阅读模式
英文:

How to get integer and fraction portions of a BigDecimal in Java

问题

对于类似于 42.7 的 BigDecimal 数字,我该如何将其分解以获得整数部分(42)和小数分数部分(0.7)?

我想要将它们都作为 BigDecimal 对象检索出来。但如果有限,我将从它们中创建 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.

答案1

得分: 2

# tl;dr

myBigDecimal
.divideAndRemainder( BigDecimal.ONE )
[ 0 ]                                    // 整数部分

and

[ 1 ]                                    // 小数部分


# `BigDecimal::divideAndRemainder`

[`BigDecimal`][1] 类提供了一些方法来实现这个目标

[`BigDecimal::divideAndRemainder`][2] 方法会将原始数除以另一个数结果是一个包含两个 `BigDecimal` 对象的数组第一个对象是整数部分第二个对象是小数部分

要实现你的目标我们只需要除以 1。`BigDecimal` 类提供了一个表示数字 1 的常量对象[`BigDecimal.ONE`][3]

```java
BigDecimal input = new BigDecimal("42.7");
BigDecimal[] parts = input.divideAndRemainder(BigDecimal.ONE);

输出到控制台。

System.out.println(Arrays.toString(parts));

> [42.0, 0.7]

从数组中提取每个部分。

BigDecimal input = new BigDecimal("42.7");
BigDecimal[] parts = input.divideAndRemainder(BigDecimal.ONE);
BigDecimal integerPart = parts[0];
BigDecimal fractionPart = parts[1];

如果你只需要其中一个部分:

  • myBigDecimal.divideAndRemainder(BigDecimal.ONE)[0] 代表整数部分
  • myBigDecimal.divideAndRemainder(BigDecimal.ONE)[1] 代表小数部分

加法 将它们重新组合

BigDecimal humptyDumpty = integerPart.add(fractionPart);


<details>
<summary>英文:</summary>

# tl;dr

    myBigDecimal
    .divideAndRemainder( BigDecimal.ONE )
    [ 0 ]                                    // for integer part

…and…

    [ 1 ]                                    // for fraction part


# `BigDecimal::divideAndRemainder`

The [`BigDecimal`][1] class offers some methods for this.

The [`BigDecimal::divideAndRemainder`][2] 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.ONE`][3]. 

```java
BigDecimal input = new BigDecimal( &quot;42.7&quot; ) ;
BigDecimal[] parts = input.divideAndRemainder( BigDecimal.ONE );

Dump to console.

System.out.println( Arrays.toString( parts ) );

>[42.0, 0.7]

Grab each piece from the array.

BigDecimal input = new BigDecimal( &quot;42.7&quot; ) ;
BigDecimal[] parts = input.divideAndRemainder( BigDecimal.ONE );
BigDecimal integerPart = parts[ 0 ] ;
BigDecimal fractionPart = parts[ 1 ] ;

If you only need one part:

  • myBigDecimal.divideAndRemainder( BigDecimal.ONE )[ 0 ] integer part
  • myBigDecimal.divideAndRemainder( BigDecimal.ONE )[ 1 ] fraction part

Addition puts them together again.

BigDecimal humptyDumpty = integerPart.add( fractionPart ) ;

答案2

得分: 2

setScale()

我认为我会选择使用 setScale()

BigDecimal bd = new BigDecimal("42.7");

BigDecimal integerPart = bd.setScale(0, RoundingMode.DOWN);
BigDecimal fractionalPart = bd.subtract(integerPart);

System.out.println(integerPart);
System.out.println(fractionalPart);

> 42
> 0.7

在我看来,这段代码很容易阅读。

英文:

setScale()

I think I’d go for setScale().

	BigDecimal bd = new BigDecimal(&quot;42.7&quot;);
	
	BigDecimal integerPart = bd.setScale(0, RoundingMode.DOWN);
	BigDecimal fractionalPart = bd.subtract(integerPart);
	
	System.out.println(integerPart);
	System.out.println(fractionalPart);

> 42
> 0.7

It seems to me to be clear to read.

huangapple
  • 本文由 发表于 2020年7月25日 14:12:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定