1 16 package org.apache.commons.lang.math; 17 18 import java.math.BigDecimal ; 19 import java.math.BigInteger ; 20 21 import org.apache.commons.lang.StringUtils; 22 23 38 public class NumberUtils { 39 40 41 public static final Long LONG_ZERO = new Long (0L); 42 43 public static final Long LONG_ONE = new Long (1L); 44 45 public static final Long LONG_MINUS_ONE = new Long (-1L); 46 47 public static final Integer INTEGER_ZERO = new Integer (0); 48 49 public static final Integer INTEGER_ONE = new Integer (1); 50 51 public static final Integer INTEGER_MINUS_ONE = new Integer (-1); 52 53 public static final Short SHORT_ZERO = new Short ((short) 0); 54 55 public static final Short SHORT_ONE = new Short ((short) 1); 56 57 public static final Short SHORT_MINUS_ONE = new Short ((short) -1); 58 59 public static final Byte BYTE_ZERO = new Byte ((byte) 0); 60 61 public static final Byte BYTE_ONE = new Byte ((byte) 1); 62 63 public static final Byte BYTE_MINUS_ONE = new Byte ((byte) -1); 64 65 public static final Double DOUBLE_ZERO = new Double (0.0d); 66 67 public static final Double DOUBLE_ONE = new Double (1.0d); 68 69 public static final Double DOUBLE_MINUS_ONE = new Double (-1.0d); 70 71 public static final Float FLOAT_ZERO = new Float (0.0f); 72 73 public static final Float FLOAT_ONE = new Float (1.0f); 74 75 public static final Float FLOAT_MINUS_ONE = new Float (-1.0f); 76 77 84 public NumberUtils() { 85 } 86 87 106 public static int stringToInt(String str) { 107 return toInt(str); 108 } 109 110 127 public static int toInt(String str) { 128 return toInt(str, 0); 129 } 130 131 149 public static int stringToInt(String str, int defaultValue) { 150 return toInt(str, defaultValue); 151 } 152 153 170 public static int toInt(String str, int defaultValue) { 171 if(str == null) { 172 return defaultValue; 173 } 174 try { 175 return Integer.parseInt(str); 176 } catch (NumberFormatException nfe) { 177 return defaultValue; 178 } 179 } 180 181 198 public static long toLong(String str) { 199 return toLong(str, 0L); 200 } 201 202 219 public static long toLong(String str, long defaultValue) { 220 if (str == null) { 221 return defaultValue; 222 } 223 try { 224 return Long.parseLong(str); 225 } catch (NumberFormatException nfe) { 226 return defaultValue; 227 } 228 } 229 230 248 public static float toFloat(String str) { 249 return toFloat(str, 0.0f); 250 } 251 252 271 public static float toFloat(String str, float defaultValue) { 272 if (str == null) { 273 return defaultValue; 274 } 275 try { 276 return Float.parseFloat(str); 277 } catch (NumberFormatException nfe) { 278 return defaultValue; 279 } 280 } 281 282 300 public static double toDouble(String str) { 301 return toDouble(str, 0.0d); 302 } 303 304 323 public static double toDouble(String str, double defaultValue) { 324 if (str == null) { 325 return defaultValue; 326 } 327 try { 328 return Double.parseDouble(str); 329 } catch (NumberFormatException nfe) { 330 return defaultValue; 331 } 332 } 333 334 370 396 public static Number createNumber(String str) throws NumberFormatException { 397 if (str == null) { 398 return null; 399 } 400 if (StringUtils.isBlank(str)) { 401 throw new NumberFormatException ("A blank string is not a valid number"); 402 } 403 if (str.startsWith("--")) { 404 return null; 409 } 410 if (str.startsWith("0x") || str.startsWith("-0x")) { 411 return createInteger(str); 412 } 413 char lastChar = str.charAt(str.length() - 1); 414 String mant; 415 String dec; 416 String exp; 417 int decPos = str.indexOf('.'); 418 int expPos = str.indexOf('e') + str.indexOf('E') + 1; 419 420 if (decPos > -1) { 421 422 if (expPos > -1) { 423 if (expPos < decPos) { 424 throw new NumberFormatException (str + " is not a valid number."); 425 } 426 dec = str.substring(decPos + 1, expPos); 427 } else { 428 dec = str.substring(decPos + 1); 429 } 430 mant = str.substring(0, decPos); 431 } else { 432 if (expPos > -1) { 433 mant = str.substring(0, expPos); 434 } else { 435 mant = str; 436 } 437 dec = null; 438 } 439 if (!Character.isDigit(lastChar)) { 440 if (expPos > -1 && expPos < str.length() - 1) { 441 exp = str.substring(expPos + 1, str.length() - 1); 442 } else { 443 exp = null; 444 } 445 String numeric = str.substring(0, str.length() - 1); 447 boolean allZeros = isAllZeros(mant) && isAllZeros(exp); 448 switch (lastChar) { 449 case 'l' : 450 case 'L' : 451 if (dec == null 452 && exp == null 453 && isDigits(numeric.substring(1)) 454 && (numeric.charAt(0) == '-' || Character.isDigit(numeric.charAt(0)))) { 455 try { 456 return createLong(numeric); 457 } catch (NumberFormatException nfe) { 458 } 460 return createBigInteger(numeric); 461 462 } 463 throw new NumberFormatException (str + " is not a valid number."); 464 case 'f' : 465 case 'F' : 466 try { 467 Float f = NumberUtils.createFloat(numeric); 468 if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) { 469 return f; 472 } 473 474 } catch (NumberFormatException nfe) { 475 } 476 case 'd' : 478 case 'D' : 479 try { 480 Double d = NumberUtils.createDouble(numeric); 481 if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) { 482 return d; 483 } 484 } catch (NumberFormatException nfe) { 485 } 486 try { 487 return createBigDecimal(numeric); 488 } catch (NumberFormatException e) { 489 } 490 default : 492 throw new NumberFormatException (str + " is not a valid number."); 493 494 } 495 } else { 496 if (expPos > -1 && expPos < str.length() - 1) { 499 exp = str.substring(expPos + 1, str.length()); 500 } else { 501 exp = null; 502 } 503 if (dec == null && exp == null) { 504 try { 506 return createInteger(str); 507 } catch (NumberFormatException nfe) { 508 } 509 try { 510 return createLong(str); 511 } catch (NumberFormatException nfe) { 512 } 513 return createBigInteger(str); 514 515 } else { 516 boolean allZeros = isAllZeros(mant) && isAllZeros(exp); 518 try { 519 Float f = createFloat(str); 520 if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) { 521 return f; 522 } 523 } catch (NumberFormatException nfe) { 524 } 525 try { 526 Double d = createDouble(str); 527 if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) { 528 return d; 529 } 530 } catch (NumberFormatException nfe) { 531 } 532 533 return createBigDecimal(str); 534 535 } 536 } 537 } 538 539 547 private static boolean isAllZeros(String str) { 548 if (str == null) { 549 return true; 550 } 551 for (int i = str.length() - 1; i >= 0; i--) { 552 if (str.charAt(i) != '0') { 553 return false; 554 } 555 } 556 return str.length() > 0; 557 } 558 559 569 public static Float createFloat(String str) { 570 if (str == null) { 571 return null; 572 } 573 return Float.valueOf(str); 574 } 575 576 585 public static Double createDouble(String str) { 586 if (str == null) { 587 return null; 588 } 589 return Double.valueOf(str); 590 } 591 592 602 public static Integer createInteger(String str) { 603 if (str == null) { 604 return null; 605 } 606 return Integer.decode(str); 608 } 609 610 619 public static Long createLong(String str) { 620 if (str == null) { 621 return null; 622 } 623 return Long.valueOf(str); 624 } 625 626 635 public static BigInteger createBigInteger(String str) { 636 if (str == null) { 637 return null; 638 } 639 return new BigInteger (str); 640 } 641 642 651 public static BigDecimal createBigDecimal(String str) { 652 if (str == null) { 653 return null; 654 } 655 if (StringUtils.isBlank(str)) { 657 throw new NumberFormatException ("A blank string is not a valid number"); 658 } 659 return new BigDecimal (str); 660 } 661 662 672 public static long min(long[] array) { 673 if (array == null) { 675 throw new IllegalArgumentException ("The Array must not be null"); 676 } else if (array.length == 0) { 677 throw new IllegalArgumentException ("Array cannot be empty."); 678 } 679 680 long min = array[0]; 682 for (int i = 1; i < array.length; i++) { 683 if (array[i] < min) { 684 min = array[i]; 685 } 686 } 687 688 return min; 689 } 690 691 699 public static int min(int[] array) { 700 if (array == null) { 702 throw new IllegalArgumentException ("The Array must not be null"); 703 } else if (array.length == 0) { 704 throw new IllegalArgumentException ("Array cannot be empty."); 705 } 706 707 int min = array[0]; 709 for (int j = 1; j < array.length; j++) { 710 if (array[j] < min) { 711 min = array[j]; 712 } 713 } 714 715 return min; 716 } 717 718 726 public static short min(short[] array) { 727 if (array == null) { 729 throw new IllegalArgumentException ("The Array must not be null"); 730 } else if (array.length == 0) { 731 throw new IllegalArgumentException ("Array cannot be empty."); 732 } 733 734 short min = array[0]; 736 for (int i = 1; i < array.length; i++) { 737 if (array[i] < min) { 738 min = array[i]; 739 } 740 } 741 742 return min; 743 } 744 745 753 public static double min(double[] array) { 754 if (array == null) { 756 throw new IllegalArgumentException ("The Array must not be null"); 757 } else if (array.length == 0) { 758 throw new IllegalArgumentException ("Array cannot be empty."); 759 } 760 761 double min = array[0]; 763 for (int i = 1; i < array.length; i++) { 764 if (array[i] < min) { 765 min = array[i]; 766 } 767 } 768 769 return min; 770 } 771 772 780 public static float min(float[] array) { 781 if (array == null) { 783 throw new IllegalArgumentException ("The Array must not be null"); 784 } else if (array.length == 0) { 785 throw new IllegalArgumentException ("Array cannot be empty."); 786 } 787 788 float min = array[0]; 790 for (int i = 1; i < array.length; i++) { 791 if (array[i] < min) { 792 min = array[i]; 793 } 794 } 795 796 return min; 797 } 798 799 809 public static long max(long[] array) { 810 if (array == null) { 812 throw new IllegalArgumentException ("The Array must not be null"); 813 } else if (array.length == 0) { 814 throw new IllegalArgumentException ("Array cannot be empty."); 815 } 816 817 long max = array[0]; 819 for (int j = 1; j < array.length; j++) { 820 if (array[j] > max) { 821 max = array[j]; 822 } 823 } 824 825 return max; 826 } 827 828 836 public static int max(int[] array) { 837 if (array == null) { 839 throw new IllegalArgumentException ("The Array must not be null"); 840 } else if (array.length == 0) { 841 throw new IllegalArgumentException ("Array cannot be empty."); 842 } 843 844 int max = array[0]; 846 for (int j = 1; j < array.length; j++) { 847 if (array[j] > max) { 848 max = array[j]; 849 } 850 } 851 852 return max; 853 } 854 855 863 public static short max(short[] array) { 864 if (array == null) { 866 throw new IllegalArgumentException ("The Array must not be null"); 867 } else if (array.length == 0) { 868 throw new IllegalArgumentException ("Array cannot be empty."); 869 } 870 871 short max = array[0]; 873 for (int i = 1; i < array.length; i++) { 874 if (array[i] > max) { 875 max = array[i]; 876 } 877 } 878 879 return max; 880 } 881 882 890 public static double max(double[] array) { 891 if (array== null) { 893 throw new IllegalArgumentException ("The Array must not be null"); 894 } else if (array.length == 0) { 895 throw new IllegalArgumentException ("Array cannot be empty."); 896 } 897 898 double max = array[0]; 900 for (int j = 1; j < array.length; j++) { 901 if (array[j] > max) { 902 max = array[j]; 903 } 904 } 905 906 return max; 907 } 908 909 917 public static float max(float[] array) { 918 if (array == null) { 920 throw new IllegalArgumentException ("The Array must not be null"); 921 } else if (array.length == 0) { 922 throw new IllegalArgumentException ("Array cannot be empty."); 923 } 924 925 float max = array[0]; 927 for (int j = 1; j < array.length; j++) { 928 if (array[j] > max) { 929 max = array[j]; 930 } 931 } 932 933 return max; 934 } 935 936 946 public static long min(long a, long b, long c) { 947 if (b < a) { 948 a = b; 949 } 950 if (c < a) { 951 a = c; 952 } 953 return a; 954 } 955 956 964 public static int min(int a, int b, int c) { 965 if (b < a) { 966 a = b; 967 } 968 if (c < a) { 969 a = c; 970 } 971 return a; 972 } 973 974 982 public static short min(short a, short b, short c) { 983 if (b < a) { 984 a = b; 985 } 986 if (c < a) { 987 a = c; 988 } 989 return a; 990 } 991 992 1000 public static byte min(byte a, byte b, byte c) { 1001 if (b < a) { 1002 a = b; 1003 } 1004 if (c < a) { 1005 a = c; 1006 } 1007 return a; 1008 } 1009 1010 1021 public static double min(double a, double b, double c) { 1022 return Math.min(Math.min(a, b), c); 1023 } 1024 1025 1036 public static float min(float a, float b, float c) { 1037 return Math.min(Math.min(a, b), c); 1038 } 1039 1040 1050 public static long max(long a, long b, long c) { 1051 if (b > a) { 1052 a = b; 1053 } 1054 if (c > a) { 1055 a = c; 1056 } 1057 return a; 1058 } 1059 1060 1068 public static int max(int a, int b, int c) { 1069 if (b > a) { 1070 a = b; 1071 } 1072 if (c > a) { 1073 a = c; 1074 } 1075 return a; 1076 } 1077 1078 1086 public static short max(short a, short b, short c) { 1087 if (b > a) { 1088 a = b; 1089 } 1090 if (c > a) { 1091 a = c; 1092 } 1093 return a; 1094 } 1095 1096 1104 public static byte max(byte a, byte b, byte c) { 1105 if (b > a) { 1106 a = b; 1107 } 1108 if (c > a) { 1109 a = c; 1110 } 1111 return a; 1112 } 1113 1114 1125 public static double max(double a, double b, double c) { 1126 return Math.max(Math.max(a, b), c); 1127 } 1128 1129 1140 public static float max(float a, float b, float c) { 1141 return Math.max(Math.max(a, b), c); 1142 } 1143 1144 1179 public static int compare(double lhs, double rhs) { 1180 if (lhs < rhs) { 1181 return -1; 1182 } 1183 if (lhs > rhs) { 1184 return +1; 1185 } 1186 long lhsBits = Double.doubleToLongBits(lhs); 1191 long rhsBits = Double.doubleToLongBits(rhs); 1192 if (lhsBits == rhsBits) { 1193 return 0; 1194 } 1195 if (lhsBits < rhsBits) { 1202 return -1; 1203 } else { 1204 return +1; 1205 } 1206 } 1207 1208 1240 public static int compare(float lhs, float rhs) { 1241 if (lhs < rhs) { 1242 return -1; 1243 } 1244 if (lhs > rhs) { 1245 return +1; 1246 } 1247 int lhsBits = Float.floatToIntBits(lhs); 1252 int rhsBits = Float.floatToIntBits(rhs); 1253 if (lhsBits == rhsBits) { 1254 return 0; 1255 } 1256 if (lhsBits < rhsBits) { 1263 return -1; 1264 } else { 1265 return +1; 1266 } 1267 } 1268 1269 1280 public static boolean isDigits(String str) { 1281 if (StringUtils.isEmpty(str)) { 1282 return false; 1283 } 1284 for (int i = 0; i < str.length(); i++) { 1285 if (!Character.isDigit(str.charAt(i))) { 1286 return false; 1287 } 1288 } 1289 return true; 1290 } 1291 1292 1305 public static boolean isNumber(String str) { 1306 if (StringUtils.isEmpty(str)) { 1307 return false; 1308 } 1309 char[] chars = str.toCharArray(); 1310 int sz = chars.length; 1311 boolean hasExp = false; 1312 boolean hasDecPoint = false; 1313 boolean allowSigns = false; 1314 boolean foundDigit = false; 1315 int start = (chars[0] == '-') ? 1 : 0; 1317 if (sz > start + 1) { 1318 if (chars[start] == '0' && chars[start + 1] == 'x') { 1319 int i = start + 2; 1320 if (i == sz) { 1321 return false; } 1323 for (; i < chars.length; i++) { 1325 if ((chars[i] < '0' || chars[i] > '9') 1326 && (chars[i] < 'a' || chars[i] > 'f') 1327 && (chars[i] < 'A' || chars[i] > 'F')) { 1328 return false; 1329 } 1330 } 1331 return true; 1332 } 1333 } 1334 sz--; int i = start; 1337 while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { 1340 if (chars[i] >= '0' && chars[i] <= '9') { 1341 foundDigit = true; 1342 allowSigns = false; 1343 1344 } else if (chars[i] == '.') { 1345 if (hasDecPoint || hasExp) { 1346 return false; 1348 } 1349 hasDecPoint = true; 1350 } else if (chars[i] == 'e' || chars[i] == 'E') { 1351 if (hasExp) { 1353 return false; 1355 } 1356 if (!foundDigit) { 1357 return false; 1358 } 1359 hasExp = true; 1360 allowSigns = true; 1361 } else if (chars[i] == '+' || chars[i] == '-') { 1362 if (!allowSigns) { 1363 return false; 1364 } 1365 allowSigns = false; 1366 foundDigit = false; } else { 1368 return false; 1369 } 1370 i++; 1371 } 1372 if (i < chars.length) { 1373 if (chars[i] >= '0' && chars[i] <= '9') { 1374 return true; 1376 } 1377 if (chars[i] == 'e' || chars[i] == 'E') { 1378 return false; 1380 } 1381 if (!allowSigns 1382 && (chars[i] == 'd' 1383 || chars[i] == 'D' 1384 || chars[i] == 'f' 1385 || chars[i] == 'F')) { 1386 return foundDigit; 1387 } 1388 if (chars[i] == 'l' 1389 || chars[i] == 'L') { 1390 return foundDigit && !hasExp; 1392 } 1393 return false; 1395 } 1396 return !allowSigns && foundDigit; 1399 } 1400 1401} 1402 | Popular Tags |