How to check a string is numeric positive?

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}$.

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. StartsWith("-")) { //string is positive }.

0.0 is not positive and it doens't have the "-" – Adel Boutros Jan 6 at 11:24 good point, well made! – ninesided Jan 6 at 11:37.

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.

1 Or boolean positive = Double. ParseDouble(string) > 0 – Peter Lawrey Jan 6 at 11:05 what if the format of the string doesn't match the number format for the current Locale? – ninesided Jan 6 at 11:09 1 @ninesided it has nothing to do with a Locale – alf Jan 6 at 11:11 @Adel Boutros:!?

OP wrote in his answer that 10.236 should return false because the number can only have "two decimal places maximum" sic... – user988052 Jan 6 at 11:15 1 @ninesided no it's not used by Double.parseDouble. It's always a decimal dot, no thousand separators are allowed, etc.Fixed format, check the docs. – alf Jan 6 at 11:25.

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.

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.

Math. Signum doesn't take a String though? – ninesided Jan 6 at 11:12 true, I was wondering why everyone suggested string.foo() until I reread the question -.

- – c00kiemon5ter Jan 6 at 11:15.

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().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions