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


Java BigDecimal toPlainString()用法及代碼示例


java.math.BigDecimal.toPlainString()方法用於表示當前的BigDecimal,通過該方法可以將此方法調用為String形式而沒有指數字段。如果值具有正刻度,則小數點右邊的位數用於指示刻度。對於零刻度或負刻度的值,將生成結果字符串,就好像該值已轉換為零刻度的數值相等的值,並且好像零刻度值的所有尾隨零都出現在結果中一樣。
注意:如果此方法的結果傳遞給構造函數,僅此BigDecimal的數值將被恢複,新BigDecimal的表示形式可能具有不同的比例。特別是,此方法的行為類似於toString方法。

用法:


public String toPlainString()

參數:此方法不接受任何參數。

返回值:此方法返回此BigDecimal數字的String表示形式,但不包含指數字段。

以下示例程序旨在說明在Java中toPlainString()方法的使用

示例1:將BigDecimal轉換為不帶指數表示法的純字符串的示例

// Java program to demonstrate 
// toPlainString() method of BigDecimal 
  
import java.math.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
        // Creating a BigDecimal object 
        BigDecimal b; 
  
        // Object of String to hold the number 
        String input = "012345678901234567"
                       + "8901234567890123"
                       + "4567890123456789"
                       + "0123456789012345"
                       + "6789012345678901"
                       + "2345678901234567"
                       + "8901234567890123"
                       + "4567890123456789"
                       + "0123456789012345"
                       + "6789012345678901"
                       + "2345678901234567"
                       + "8901234567890123"
                       + "4567890123456789"
                       + "0123456789012345"
                       + "6789012345678901"
                       + "2345678901234567"
                       + "8901234567890123"
                       + "4554324324362432"
                       + "7674637264783264"
                       + "7832678463726478"
                       + "3264736274673864"
                       + "7364732463546354"
                       + "6354632564532645"
                       + "6325463546536453"
                       + "6546325463546534"
                       + "6325465345326456"
                       + "4635463263453264"
                       + "654632498739473"; 
  
        // Converting to BigDecimal 
        b = new BigDecimal(input); 
  
        // Apply toPlainString() method 
        String s = b.toPlainString(); 
  
        // Print the result 
        System.out.println(s); 
    } 
}
輸出:

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264783264736274673864736473246354635463546325645326456325463546536453654632546354653463254653453264564635463263453264654632498739473

示例2:使用指數符號將BigDecimal轉換為純字符串的示例

// Java program to demonstrate 
// toPlainString() method of BigDecimal 
  
import java.math.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Create a BigDecimal object 
        BigDecimal a; 
  
        // Create a String object 
        String s; 
  
        a = new BigDecimal("4536785E10"); 
  
        // apply toPlainString() method 
        s = a.toPlainString(); 
  
        // print the result 
        System.out.println(s); 
    } 
}
輸出:

45367850000000000

參考文獻: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#toPlainString()



相關用法


注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigDecimal toPlainString() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。