For example I need 5.0
to become 5
, or 4.3000
to become 4.3
.
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.
Algorithm. Step 1: Get the string Step 2: Count number of trailing zeros n Step 3: Remove n characters from the beginning Step 4: return remaining string.
Removing Same Number of trailing zeros by Formula Select the adjacent cell to the number you used. Type this formula =LEFT(D1, LEN(D4)-2)*1, D4 is the cell you will remove trailing zeros from, 2 is the number of zeros you want to remove.
DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits.
You should use DecimalFormat("0.#")
For 4.3000
Double price = 4.3000; DecimalFormat format = new DecimalFormat("0.#"); System.out.println(format.format(price));
output is:
4.3
In case of 5.000 we have
Double price = 5.000; DecimalFormat format = new DecimalFormat("0.#"); System.out.println(format.format(price));
And the output is:
5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With