Java.math.BigDecimal.stripTrailingZeros() Method


Advertisements


Description

The java.math.BigDecimal.stripTrailingZeros() returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.

For example, stripping the trailing zeros from the BigDecimal value 600.0, which has [BigInteger, scale] components equals to [6000, 1], yields 6E2 with [BigInteger, scale] components equals to [6, -2].

Declaration

Following is the declaration for java.math.BigDecimal.stripTrailingZeros() method.

public BigDecimal stripTrailingZeros()

Parameters

NA

Return Value

This method returns a numerically equal BigDecimal with any trailing zeros removed.

Exception

NA

Example

The following example shows the usage of math.BigDecimal.stripTrailingZeros() method.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 4 BigDecimal objects
      BigDecimal bg1, bg2, bg3, bg4;

      bg1 = new BigDecimal("235.000");
      bg2 = new BigDecimal("23500");

      // assign the result of stripTrailingZeros method to bg3, bg4
      bg3 = bg1.stripTrailingZeros();
      bg4 = bg2.stripTrailingZeros();

      String str1 = bg1 + " after removing trailing zeros " +bg3;
      String str2 = bg2 + " after removing trailing zeros " +bg4;

      // print bg3, bg4 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Let us compile and run the above program, this will produce the following result −

235.000 after removing trailing zeros 235
23500 after removing trailing zeros 2.35E+4

java_math_bigdecimal.htm

Advertisements