trim BigDecimal to cut off all trailing zeros. - Java java.math

Java examples for java.math:BigDecimal Format

Description

trim BigDecimal to cut off all trailing zeros.

Demo Code

/*//from www  .  j a  va 2  s .  c  o m
 * BigDecimalHelper.java
 * Copyright 2003 (C) Jonas Karlsson <jujutsunerd@sf.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Created on April 12, 2003, 3:20 AM
 */
//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal n = new BigDecimal("1234");
        System.out.println(trimBigDecimal(n));
    }

    /**
     * trimBigDecimal ( (BigDecimal) a) to cut off all trailing zeros.
     * It's a terrible hack.
     * @param n the BigDecimal to trim all trailing zeros from
     * @return the trimmed BigDecimal
     */
    public static BigDecimal trimBigDecimal(BigDecimal n) {
        if (n.unscaledValue().intValue() == 0) {
            // Java 1.5 will not throw an ArthmeticException if you change the
            // scale of 0.0 to 0, so it will keep going through the loop below
            // forever. To get around this we test for the special case here.
            return BigDecimal.ZERO;
        }

        if (n.scale() <= 0) {
            return n;
        }

        BigDecimal stripped = n.stripTrailingZeros();
        if (stripped.scale() < 0) {
            stripped = n.setScale(0);
        }

        return stripped;
    }
}

Related Tutorials