validate input string as double value - Java java.lang

Java examples for java.lang:double Format

Description

validate input string as double value

Demo Code


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String amount = "java2s.com";
        System.out.println(valid(amount));
    }/* w w  w.  j  a  v a2  s.  c  o m*/
    
    public static boolean valid(String amount) {
        Pattern pattern = Pattern.compile("^(-)?(\\d*)(\\.(\\d*))?$");
        Matcher matcher = pattern.matcher(amount);
        return matcher.matches();
    }
}

Related Tutorials