Java BigDecimal removeTrailingZeros(final BigDecimal bd)

Here you can find the source of removeTrailingZeros(final BigDecimal bd)

Description

remove Trailing Zeros

License

Open Source License

Declaration

public static BigDecimal removeTrailingZeros(final BigDecimal bd) 

Method Source Code


//package com.java2s;
/*//from  w  w  w.  j a  va2  s . c  o  m
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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.
*
* Copyright (c) 2006 - 2013 Pentaho Corporation and Contributors.  All rights reserved.
*/

import java.math.BigDecimal;

public class Main {
    public static BigDecimal removeTrailingZeros(final BigDecimal bd) {
        if (bd.signum() == 0) {
            return bd.setScale(0);
        }

        final String text = bd.toPlainString(); // get non-logarithm representation
        int scale = bd.scale();
        for (int i = text.length() - 1; i >= 0; i--) {
            final char c = text.charAt(i);
            if (c != '0') {
                break;
            }
            scale -= 1;
        }
        return bd.setScale(scale);
    }
}

Related

  1. rangedValue(BigDecimal lower, BigDecimal upper, Random random)
  2. readBigDecimal(byte valueBytes[], int valueLength, int scale)
  3. readBigDecimal(DataInput in)
  4. readBigDecimal(final Object object)
  5. releaseNull(BigDecimal amount)
  6. removeVAT(int priceInCents, BigDecimal vat)
  7. reverseSign(BigDecimal decimal)
  8. rundeKaufmaennisch( BigDecimal bigDecimal, int stellen)
  9. safeAdd(BigDecimal left, BigDecimal right)