Java BigDecimal from String stringToDec(final CharSequence stringValue, final BigDecimal defaultValue)

Here you can find the source of stringToDec(final CharSequence stringValue, final BigDecimal defaultValue)

Description

Converts a string value to its equivalent BigDecimal value.

License

Apache License

Parameter

Parameter Description
stringValue The string that will be converted.
defaultValue The value that will be returned if the string is invalid.

Return

the BigDecimal value (using the BigDecimal constructor or the defaultValue if the string is not a numeric value).

Declaration

public static BigDecimal stringToDec(final CharSequence stringValue, final BigDecimal defaultValue) 

Method Source Code


//package com.java2s;
/*//from  w  ww  . ja  v  a  2  s.c o  m
 * Copyright 2006-2014 Andy King (GreatLogic.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;

public class Main {
    /**
     * Converts a string value to its equivalent BigDecimal value. If the string is not a valid numeric
     * value then BigDecimal.ZERO is returned. If there are commas in the value then they will be
     * removed before converting to a decimal (this makes this non-portable to some locations).
     * @param stringValue The string that will be converted.
     * @return the BigDecimal value or BigDecimal.ZERO if the value is not valid.
     */
    public static BigDecimal stringToDec(final String stringValue) {
        return stringToDec(stringValue, BigDecimal.ZERO);
    }

    /**
     * Converts a string value to its equivalent BigDecimal value. If the string is not a valid numeric
     * value then the default value is returned. If there are commas in the value then they will be
     * removed before converting to a decimal (this makes this non-portable to some locations).
     * @param stringValue The string that will be converted.
     * @param defaultValue The value that will be returned if the string is invalid.
     * @return the BigDecimal value (using the BigDecimal constructor or the defaultValue if the string
     * is not a numeric value).
     */
    public static BigDecimal stringToDec(final CharSequence stringValue, final BigDecimal defaultValue) {
        BigDecimal result;
        try {
            result = new BigDecimal(stringToNumericAdjust(stringValue, false));
        } catch (final Exception e) {
            result = defaultValue;
        }
        return result;
    }

    private static String stringToNumericAdjust(final CharSequence stringValue, final boolean removeDecimals) {
        String result = stringValue.toString();
        if (result.indexOf(',') > 0) {
            result = result.replace(",", "");
        }
        if (result.length() > 0 && (result.charAt(0) == ' ' || result.charAt(result.length() - 1) == ' ')) {
            result = result.trim();
        }
        if (result.endsWith("-") && result.length() > 1) {
            result = "-" + result.substring(0, result.length() - 1);
        }
        if (result.startsWith("+") && result.length() > 1) {
            result = result.substring(1);
        }
        if (result.length() > 0 && (result.charAt(0) == ' ' || result.charAt(result.length() - 1) == ' ')) {
            result = result.trim();
        }
        if (removeDecimals) {
            final int dotIndex = result.indexOf('.');
            boolean validDecimal = true;
            if (dotIndex >= 0 && result.length() > dotIndex + 1) {
                final String decimal = result.substring(dotIndex + 1);
                try {
                    Long.parseLong(decimal);
                } catch (final Exception e) {
                    validDecimal = false;
                }
            }
            if (validDecimal) {
                if (dotIndex == 0) {
                    result = "0";
                } else if (dotIndex > 0) {
                    result = result.substring(0, dotIndex);
                }
            }
        }
        return result;
    }
}

Related

  1. string2BigDecimal(String s)
  2. string2BigDecimal(String st)
  3. String2BigDecimal(String str)
  4. stringToBigDecimal(String numberString)
  5. stringToBigDecimal(String src, int[] endPos)