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


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


java.math.BigDecimal.toEngineeringString()方法用於表示當前的BigDecimal,如果需要指數,可以使用工程符號將其通過String形式調用。 BigDecimal的字符串表示形式與toString()方法中描述的字符串表示形式相同,不同之處在於,如果使用指數表示法,則將十的冪調整為三的倍數(工程表示法),以使非零值的整數部分為範圍是1到999。
用法:

public String toEngineeringString()

參數:此方法不接受任何參數。
返回值:此方法返回此BigDecimal數字的工程字符串表示形式。


以下示例程序旨在說明在Java中toEngineeringString()方法的使用
示例1:將BigDecimal轉換為不帶指數表示法的工程字符串的示例

// Java program to demonstrate 
// toEngineeringString() 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"
                       + "4635463263453264"
                       + "654632498739473"; 
  
        // Converting to BigDecimal 
        b = new BigDecimal(input); 
  
        // Apply toEngineeringString() method 
        String s = b.toEngineeringString(); 
  
        // Print the result 
        System.out.println(s); 
    } 
}
輸出:

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264784635463263453264654632498739473

示例2:使用指數符號將BigDecimal轉換為Engineering String的示例

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

45.36785E+15

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



相關用法


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