# Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.79 KB  |  hits: 19  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. android parsing negative number strings
2. -123.23
3. (123.23)
4. 123.23-
5.
6. Double newNumber = 0;
7. if(number.charAt(i).isDigit()){
8.      //parse numeber here using WarrenFaiths method and place the int or float or double
9.      newNumber = Double.valueOf(number);
10. }else{
11.     Boolean negative = false;
12.
13.     if(number.startsWith("-") || number.endsWith("-")){
14.          number.replace("-", "");
15.          negative = true;
16.     }
17.     if(number.startsWith("(") || number.endsWith(")"){
18.         number.replace("(", "");
19.         number.replace(")", "");
20.     }
21.
22.     //parse numeber here using WarrenFaiths method and place the float or double
23.     newNumber = Double.valueOf(number);
24.
25.     if(negative){
26.       newNumber = newNumber * -1;
27.     }
28. }
29.
30. private Double RegNumber(String sIn) {
31.         Double result = 0E0;
32.         Pattern p = Pattern.compile("^\-?([0-9]{0,3}(\,?[0-9]{3})*(\.?[0-9]*))");   // -1,123.11
33.         Matcher m = p.matcher(sIn);
34.         if (m.matches()) {
35.             result = Double.valueOf(sIn);
36.         } else {
37.             Pattern p1 = Pattern.compile("^\(?([0-9]{0,3}(\,?[0-9]{3})*(\.?[0-9]*))\)?\$");  // (1,123.22)
38.             Matcher m1 = p1.matcher(sIn);
39.             if (m1.matches()) {
40.                 String x[] = sIn.split("(\({1})|(\){1})"); // extract number
41.                 result = Double.valueOf("-" + x[1]);
42.             } else {
43.                 Pattern p2 = Pattern.compile("([0-9]{0,3}(\,?[0-9]{3})*(\.?[0-9]*))\-\$");  // 1,123.22-
44.                 Matcher m2 = p2.matcher(sIn);
45.                 if (m2.matches()) {
46.                     String x[] = sIn.split("(\-{1})"); // extract number
47.                     result = Double.valueOf("-" + x[0]);
48.                 } else {
49.                     result = 99999999E99;  // error, big value
50.                 }
51.             }
52.         }
53.         return result;
54.     }