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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。