📜  Java中的 BigDecimal stripTrailingZeros() 方法

📅  最后修改于: 2022-05-13 01:54:41.971000             🧑  作者: Mango

Java中的 BigDecimal stripTrailingZeros() 方法

Java.math.BigDecimal.stripTrailingZeros()是Java中的一个内置方法,它返回一个 BigDecimal,它在数字上等于这个值,但从表示中删除了任何尾随零。所以基本上该函数从 BigDecimal 值中修剪掉尾随零。

句法:

public BigDecimal stripTrailingZeros()

参数:此方法不接受任何参数。

返回值:此方法返回一个等于 BigDecimal 的数值,其中所有尾随零都被删除。

例子:

Input: 785.000
Output: 785

Input: 125500000
Output: 1.255E+8

下面的程序说明了上述方法的工作:
方案一:

// Program to demonstrate stripTrailingZeros() method of BigDecimal 
  
import java.math.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
  
        BigDecimal b1 = new BigDecimal("785.000");
        BigDecimal b2 = new BigDecimal("125500");
  
        // Assigning the result of stripTrailingZeros method
        // to BigDecimal objects b3, b4
        BigDecimal b3 = b1.stripTrailingZeros();
        BigDecimal b4 = b2.stripTrailingZeros();
  
        // print b3, b4 values
        System.out.println(b1 + " after removing trailing zeros " + b3);
        System.out.println(b2 + " after removing trailing zeros " + b4);
    }
}
输出:
785.000 after removing trailing zeros 785
125500 after removing trailing zeros 1.255E+5

方案二:

// Program to demonstrate stripTrailingZeros() method of BigDecimal 
  
  
import java.math.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        BigDecimal b1 = new BigDecimal("785.00000");
        BigDecimal b2 = new BigDecimal("125500000");
  
        // Assigning the result of stripTrailingZeros method
        // to BigDecimal objects b3, b4
        BigDecimal b3 = b1.stripTrailingZeros();
        BigDecimal b4 = b2.stripTrailingZeros();
  
        // Printing b3, b4 values
        System.out.println(b1 + " after removing trailing zeros " + b3);
        System.out.println(b2 + " after removing trailing zeros " + b4);
    }
}
输出:
785.00000 after removing trailing zeros 785
125500000 after removing trailing zeros 1.255E+8

参考: https: Java/math/BigDecimal.html#stripTrailingZeros()