當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。