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


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