| 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  
|