The java.math.BigDecimal.stripTrailingZeros() is an inbuilt method in Java that returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. So basically the function trims off the trailing zero from the BigDecimal value.
Syntax:
public BigDecimal stripTrailingZeros()
Parameter: This method does not accepts any parameter.
Return value: This method returns a numerical value equal to the BigDecimal with all the trailing zeros being removed.
Examples:
Input: 785.000 Output: 785 Input: 125500000 Output: 1.255E+8
Below programs illustrate the working of the above mentioned method:
Program 1:
// Program to demonstrate stripTrailingZeros() method of BigDecimal import java.math.*; public class Gfg { public static void main(String[] args) { BigDecimal b1 = new BigDecimal("785.000"); BigDecimal b2 = new BigDecimal("125500"); // Assigning the result of stripTrailingZeros method // to BigDecimal objects b3, b4 BigDecimal b3 = b1.stripTrailingZeros(); BigDecimal b4 = b2.stripTrailingZeros(); // print b3, b4 values System.out.println(b1 + " after removing trailing zeros " + b3); System.out.println(b2 + " after removing trailing zeros " + b4); } } |
785.000 after removing trailing zeros 785 125500 after removing trailing zeros 1.255E+5
Program 2:
// Program to demonstrate stripTrailingZeros() method of BigDecimal import java.math.*; public class gfg { public static void main(String[] args) { BigDecimal b1 = new BigDecimal("785.00000"); BigDecimal b2 = new BigDecimal("125500000"); // Assigning the result of stripTrailingZeros method // to BigDecimal objects b3, b4 BigDecimal b3 = b1.stripTrailingZeros(); BigDecimal b4 = b2.stripTrailingZeros(); // Printing b3, b4 values System.out.println(b1 + " after removing trailing zeros " + b3); System.out.println(b2 + " after removing trailing zeros " + b4); } } |
785.00000 after removing trailing zeros 785 125500000 after removing trailing zeros 1.255E+8
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#stripTrailingZeros()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- BigDecimal min() Method in Java
- BigDecimal max() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal valueOf() Method in Java
- BigDecimal byteValueExact() Method in Java
- BigDecimal doubleValue() Method in Java
- BigDecimal ulp() Method in Java
- BigDecimal toBigIntegerExact() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal equals() Method in Java
- BigDecimal shortValueExact() Method in Java
- BigDecimal signum() Method in Java
- BigDecimal longValue() Method in Java
- BigDecimal hashCode() Method in Java
- BigDecimal precision() Method in Java
- BigDecimal intValue() Method in Java
- BigDecimal longValueExact() Method in Java
- BigDecimal intvalueExact() Method in Java
- BigDecimal round() Method in Java
- BigDecimal scale() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

