Java BigDecimal getWithoutTrailingZeroes(@Nullable final BigDecimal aValue)

Here you can find the source of getWithoutTrailingZeroes(@Nullable final BigDecimal aValue)

Description

Get the passed BigDecimal without any trailing zeroes.

License

Apache License

Parameter

Parameter Description
aValue The BigDecimal to be modified. May be <code>null</code>.

Return

null if the input value is null.

Declaration

@Nullable
@CheckReturnValue
public static BigDecimal getWithoutTrailingZeroes(@Nullable final BigDecimal aValue) 

Method Source Code

//package com.java2s;
/**/*from  w  ww  .  ja va2  s  .co m*/
 * Copyright (C) 2014-2017 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigDecimal;

import javax.annotation.CheckReturnValue;

import javax.annotation.Nullable;

public class Main {
    /**
     * Get the passed String as a BigDecimal without any trailing zeroes.
     *
     * @param sValue
     *        The String to be used as a BigDecimal to be modified. May be
     *        <code>null</code>.
     * @return <code>null</code> if the input value is <code>null</code>.
     */
    @Nullable
    @CheckReturnValue
    public static BigDecimal getWithoutTrailingZeroes(@Nullable final String sValue) {
        if (sValue == null)
            return null;

        return getWithoutTrailingZeroes(new BigDecimal(sValue));
    }

    /**
     * Get the passed BigDecimal without any trailing zeroes. Examples:
     * <ul>
     * <li>new BigDecimal ("0.00000000") --&gt; 0</li>
     * <li>new BigDecimal ("10") --&gt; 10</li>
     * <li>new BigDecimal ("10.00000000") --&gt; 10</li>
     * <li>new BigDecimal ("10.1") --&gt; 10.1</li>
     * <li>new BigDecimal ("10.10000000") --&gt; 10.1</li>
     * <li>new BigDecimal ("10.345") --&gt; 10.345</li>
     * <li>new BigDecimal ("10.3450000000") --&gt; 10.345</li>
     * </ul>
     *
     * @param aValue
     *        The BigDecimal to be modified. May be <code>null</code>.
     * @return <code>null</code> if the input value is <code>null</code>.
     */
    @Nullable
    @CheckReturnValue
    public static BigDecimal getWithoutTrailingZeroes(@Nullable final BigDecimal aValue) {
        if (aValue == null)
            return null;

        // stripTrailingZeros does not work for "0"!
        if (BigDecimal.ZERO.compareTo(aValue) == 0)
            return BigDecimal.ZERO;

        final BigDecimal ret = aValue.stripTrailingZeros();
        // Avoid stuff like "6E2"
        return ret.scale() >= 0 ? ret : ret.setScale(0);
    }
}

Related

  1. getUnscaledBytes(BigDecimal bd)
  2. getUpdateAccountsQuery(BigDecimal[] result, int id)
  3. getUserLink(BigDecimal id, String name)
  4. getValue(BigDecimal value, Class clazz)
  5. getValue(String key, Map valuesMap)
  6. hash(BigDecimal bd)
  7. hasSignedChanged(final BigDecimal v1, final BigDecimal v2)
  8. htmlFooter(String providerNo, int count, BigDecimal total)
  9. humanReadableByteCount(BigDecimal bytes, Boolean si)