Java BigDecimal to convertToBigDecimal(String value)

Here you can find the source of convertToBigDecimal(String value)

Description

Convert a string representing a decimal value into a BigDecimal object

License

Open Source License

Parameter

Parameter Description
value the decimal value as a string

Return

the BigDecimal object representing the value or null in case of conversion error

Declaration

public static BigDecimal convertToBigDecimal(String value) 

Method Source Code

//package com.java2s;
/**//w  ww  .ja  v a  2  s.com
 * Copyright (c) 2014-2017 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Convert a string representing a decimal value into a BigDecimal object
     *
     * @param value the decimal value as a string
     *
     * @return the BigDecimal object representing the value or null in case of conversion error
     */
    public static BigDecimal convertToBigDecimal(String value) {
        BigDecimal result = null;
        if (isValid(value)) {
            result = new BigDecimal(value.trim());
        }
        return result;
    }

    private static boolean isValid(String value) {
        return (value != null) && !value.isEmpty() && !value.equalsIgnoreCase("N/A")
                && !value.equalsIgnoreCase("NA") && !value.equals("-") && !value.equals("--");
    }
}

Related

  1. convertToBigDecimal(final Object o)
  2. convertToBigDecimal(Object sourceValue)
  3. convertToBigDecimal(String s)
  4. convertToBigDecimal(String val)
  5. convertToBigDecimal(String value)
  6. convertToFen(BigDecimal num1)
  7. decimalToBytes(BigDecimal v, byte[] result, final int offset, int length)
  8. decimalToString(BigDecimal value)
  9. toBeforeDecimalPointString(BigDecimal bigDecimal)