2

How to check a string is numeric positive and possibly a comma as decimal separator and two decimal places maximum.

Example

10.25 is true 10.2 is true 10.236 is false theres 3 decimal 10.dee is false

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Mercer
  • 9,736
  • 30
  • 105
  • 170

7 Answers7

6

or you can use this regexp

^[0-9]*([,]{1}[0-9]{0,2}){0,1}$

if you want both comma and dot as allowed separator then

^[0-9]*([\.,]{1}[0-9]{0,2}){0,1}$
czajah
  • 419
  • 4
  • 12
4

If a string represents negative number then it must be prefixed with a minus sign, regardless of the precision, number format or decimal separator used:

if(string.equals("0.0") || !string.startsWith("-"))  {
    //string is positive
}
ninesided
  • 23,085
  • 14
  • 83
  • 107
3

To check for numeric, use:

Double number = Double.parseDouble(string);
return number > 0;

**UPDATE:

For comma as seperator, you can use the following:

NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse(string);
double d = number.doubleValue();
return number > 0;
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
0

Use that returns true if is a positive integer and false if not.

public static boolean isPositiveInteger(String str) {

    if (str == null) {
        return false;
    }
    int length = str.length();
    if (length == 0) {
        return false;
    }
    if (str.charAt(0) == '-') {
            return false;
    }
    for (int i = 0; i < length; i++) {
        char c = str.charAt(i);
        boolean isDigit = (c >= '0' && c <= '9');
        if (!isDigit) {
            return false;
        }
    }
    return true;
}
ekad
  • 14,436
  • 26
  • 44
  • 46
Leandro P.
  • 165
  • 2
  • 5
0

My suggestion: Convert to a double first. That will test for numeric. If that fails; convert the last comma in a period (indexOf, replace). Then convert again.

For making sure you have 2 decimal places maximum - there's functions for that in DecimalFormat... But for alternatives, see this question here. Rounding a double to 5 decimal places in Java ME

After you have the double converted and stored to 2 decimal places, you can check if it's negative.

Community
  • 1
  • 1
Pimgd
  • 5,983
  • 1
  • 30
  • 45
0

The correct way to get the sign of a number is to use signum provided by Math lib.

Math.signum(yourDouble);

Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
0

Another option is to use BigDecimal:

import java.math.BigDecimal;

public class TestBigDecimal2 {

    public static void main(String[] args) {
        String[] values = { "10.25", "-10.25", "10.2", "10.236", "10.dee" };
        for(String value : values) {
            System.out.println(value + " is valid: " + checkValid(value));
        }
    }

    private static boolean checkValid(String value) {
        try {
            BigDecimal decimal = new BigDecimal(value);
            return decimal.signum() > 0 && decimal.scale() < 3;
        }
        catch (NumberFormatException e) {
            return false;
        }
    }
}
Walter Laan
  • 2,956
  • 17
  • 15