Here you can find the source of getWithoutTrailingZeroes(@Nullable final BigDecimal aValue)
Parameter | Description |
---|---|
aValue | The BigDecimal to be modified. May be <code>null</code>. |
null
if the input value is null
.
@Nullable @CheckReturnValue public static BigDecimal getWithoutTrailingZeroes(@Nullable final BigDecimal aValue)
//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") --> 0</li> * <li>new BigDecimal ("10") --> 10</li> * <li>new BigDecimal ("10.00000000") --> 10</li> * <li>new BigDecimal ("10.1") --> 10.1</li> * <li>new BigDecimal ("10.10000000") --> 10.1</li> * <li>new BigDecimal ("10.345") --> 10.345</li> * <li>new BigDecimal ("10.3450000000") --> 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); } }