BigDecimal 불필요한 자리수 0 제거하기

만년 쭈글이 개발자·2022년 9월 6일
0

TIL

목록 보기
5/13

BigDecimal.stripTrailingZeros();

    /**
     * Returns a {@code 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 {@code BigDecimal} value {@code 600.0}, which has
     * [{@code BigInteger}, {@code scale}] components equals to
     * [6000, 1], yields {@code 6E2} with [{@code BigInteger},
     * {@code scale}] components equals to [6, -2].  If
     * this BigDecimal is numerically equal to zero, then
     * {@code BigDecimal.ZERO} is returned.
     *
     * @return a numerically equal {@code BigDecimal} with any
     * trailing zeros removed.
     * @since 1.5
     */
    public BigDecimal stripTrailingZeros() {
        if (intCompact == 0 || (intVal != null && intVal.signum() == 0)) {
            return BigDecimal.ZERO;
        } else if (intCompact != INFLATED) {
            return createAndStripZerosToMatchScale(intCompact, scale, Long.MIN_VALUE);
        } else {
            return createAndStripZerosToMatchScale(intVal, scale, Long.MIN_VALUE);
        }
    }

0을 제거해서 반환해줌
개꿀메소드
scale 처리나 자리수 반올림/내림 없이 값에 일괄적으로 붙는 0 떼고싶을때

toPlainString/toString 하기전에 하면 0 싹 없어짐
지수표현 안하고 싶을때 toPlainString
거의 toPlainString으로만 쓸일이 더 많은것 같음

profile
오늘의 나는 내일의 나보다 젊지

0개의 댓글