Java Decimal Format isValidDouble(String value, int decimals)

Here you can find the source of isValidDouble(String value, int decimals)

Description

Tell whether a double string is in valid format, according with a number of fraction digits.

License

Apache License

Parameter

Parameter Description
value the double string to be tested
decimals number of decimals

Return

true if this double string is valid; false otherwise

Declaration

public static boolean isValidDouble(String value, int decimals) 

Method Source Code

//package com.java2s;
/**/* w  w w  . j av a 2  s. c  o m*/
 * Copyright (C) 2012 Red Hat, Inc. and/or its affiliates.
 *
 * 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.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParsePosition;
import java.util.Locale;

public class Main {
    /**
     * Tell whether a double string is in valid format, according with
     * a number of fraction digits.
     *
     * @param value    the double string to be tested
     * @param decimals number of decimals
     * @return true if this double string is valid; false otherwise
     */
    public static boolean isValidDouble(String value, int decimals) {
        DecimalFormat df;
        df = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
        DecimalFormatSymbols dsym = df.getDecimalFormatSymbols();
        char decSepar = dsym.getDecimalSeparator();

        if (value == null || value.trim().equals(""))
            return true;

        value = value.trim();

        if (value.indexOf(decSepar) != value.lastIndexOf(decSepar))
            return false;

        if (value.indexOf(decSepar) != -1 && decimals != -1
                && (value.length() - value.indexOf(decSepar) - 1) > decimals)
            return false;

        DecimalFormat numberFormat = getFormat(decimals);

        try {
            ParsePosition pos = new ParsePosition(0);
            Number number = numberFormat.parse(value, pos);
            if (number == null || pos.getIndex() != value.length())
                return false;

            return true;
        } catch (Exception except) {
            return false;
        }
    }

    /**
     * Returns the decimal formatter, which has a number of decimals.
     *
     * @param decimals number of decimals
     * @return desired DecimalFormat
     */
    private static DecimalFormat getFormat(int decimals) {
        DecimalFormat df;
        df = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
        DecimalFormatSymbols dsym = df.getDecimalFormatSymbols();
        char decimalSeparator = dsym.getDecimalSeparator();

        StringBuffer pattern = new StringBuffer("0");
        pattern.append(decimalSeparator);
        for (int i = 0; i < decimals; i++)
            pattern.append("0");

        DecimalFormat numberFormat = new DecimalFormat(pattern.toString());
        DecimalFormatSymbols decimalSymb;
        decimalSymb = numberFormat.getDecimalFormatSymbols();

        numberFormat.setGroupingSize(50);
        decimalSymb.setDecimalSeparator(decimalSeparator);
        numberFormat.setDecimalFormatSymbols(decimalSymb);

        return numberFormat;
    }
}

Related

  1. getTwoPoint(double val)
  2. getValue(double value)
  3. isDouble(String _str)
  4. isLikelyDouble(long value)
  5. isprime(double x)
  6. Julian_Cal(double JDN)
  7. listToString(List list)
  8. numToString(double num)
  9. pad(double n)