当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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

以下示例程序旨在说明上述方法的用法方式:
示例1:

// 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

示例2:

// 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://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#stripTrailingZeros()



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 BigDecimal stripTrailingZeros() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。