1 16 package org.apache.commons.lang; 17 18 import java.math.BigDecimal ; 19 import java.math.BigInteger ; 20 21 36 public final class NumberUtils { 37 39 46 public NumberUtils() { 47 } 48 49 51 59 public static int stringToInt(String str) { 60 return stringToInt(str, 0); 61 } 62 63 71 public static int stringToInt(String str, int defaultValue) { 72 try { 73 return Integer.parseInt(str); 74 } catch (NumberFormatException nfe) { 75 return defaultValue; 76 } 77 } 78 79 81 116 137 public static Number createNumber(String val) throws NumberFormatException { 138 if (val == null) { 139 return null; 140 } 141 if (val.length() == 0) { 142 throw new NumberFormatException ("\"\" is not a valid number."); 143 } 144 if (val.startsWith("--")) { 145 return null; 150 } 151 if (val.startsWith("0x") || val.startsWith("-0x")) { 152 return createInteger(val); 153 } 154 char lastChar = val.charAt(val.length() - 1); 155 String mant; 156 String dec; 157 String exp; 158 int decPos = val.indexOf('.'); 159 int expPos = val.indexOf('e') + val.indexOf('E') + 1; 160 161 if (decPos > -1) { 162 163 if (expPos > -1) { 164 if (expPos < decPos) { 165 throw new NumberFormatException (val + " is not a valid number."); 166 } 167 dec = val.substring(decPos + 1, expPos); 168 } else { 169 dec = val.substring(decPos + 1); 170 } 171 mant = val.substring(0, decPos); 172 } else { 173 if (expPos > -1) { 174 mant = val.substring(0, expPos); 175 } else { 176 mant = val; 177 } 178 dec = null; 179 } 180 if (!Character.isDigit(lastChar)) { 181 if (expPos > -1 && expPos < val.length() - 1) { 182 exp = val.substring(expPos + 1, val.length() - 1); 183 } else { 184 exp = null; 185 } 186 String numeric = val.substring(0, val.length() - 1); 188 boolean allZeros = isAllZeros(mant) && isAllZeros(exp); 189 switch (lastChar) { 190 case 'l' : 191 case 'L' : 192 if (dec == null 193 && exp == null 194 && isDigits(numeric.substring(1)) 195 && (numeric.charAt(0) == '-' || Character.isDigit(numeric.charAt(0)))) { 196 try { 197 return createLong(numeric); 198 } catch (NumberFormatException nfe) { 199 } 201 return createBigInteger(numeric); 202 203 } 204 throw new NumberFormatException (val + " is not a valid number."); 205 case 'f' : 206 case 'F' : 207 try { 208 Float f = NumberUtils.createFloat(numeric); 209 if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) { 210 return f; 213 } 214 215 } catch (NumberFormatException nfe) { 216 } 217 case 'd' : 219 case 'D' : 220 try { 221 Double d = NumberUtils.createDouble(numeric); 222 if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) { 223 return d; 224 } 225 } catch (NumberFormatException nfe) { 226 } 227 try { 228 return createBigDecimal(numeric); 229 } catch (NumberFormatException e) { 230 } 231 default : 233 throw new NumberFormatException (val + " is not a valid number."); 234 235 } 236 } else { 237 if (expPos > -1 && expPos < val.length() - 1) { 240 exp = val.substring(expPos + 1, val.length()); 241 } else { 242 exp = null; 243 } 244 if (dec == null && exp == null) { 245 try { 247 return createInteger(val); 248 } catch (NumberFormatException nfe) { 249 } 250 try { 251 return createLong(val); 252 } catch (NumberFormatException nfe) { 253 } 254 return createBigInteger(val); 255 256 } else { 257 boolean allZeros = isAllZeros(mant) && isAllZeros(exp); 259 try { 260 Float f = createFloat(val); 261 if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) { 262 return f; 263 } 264 } catch (NumberFormatException nfe) { 265 } 266 try { 267 Double d = createDouble(val); 268 if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) { 269 return d; 270 } 271 } catch (NumberFormatException nfe) { 272 } 273 274 return createBigDecimal(val); 275 276 } 277 278 } 279 } 280 281 289 private static boolean isAllZeros(String s) { 290 if (s == null) { 291 return true; 292 } 293 for (int i = s.length() - 1; i >= 0; i--) { 294 if (s.charAt(i) != '0') { 295 return false; 296 } 297 } 298 return s.length() > 0; 299 } 300 301 303 310 public static Float createFloat(String val) { 311 return Float.valueOf(val); 312 } 313 314 321 public static Double createDouble(String val) { 322 return Double.valueOf(val); 323 } 324 325 333 public static Integer createInteger(String val) { 334 return Integer.decode(val); 336 } 337 338 345 public static Long createLong(String val) { 346 return Long.valueOf(val); 347 } 348 349 356 public static BigInteger createBigInteger(String val) { 357 BigInteger bi = new BigInteger (val); 358 return bi; 359 } 360 361 368 public static BigDecimal createBigDecimal(String val) { 369 BigDecimal bd = new BigDecimal (val); 370 return bd; 371 } 372 373 375 383 public static long minimum(long a, long b, long c) { 384 if (b < a) { 385 a = b; 386 } 387 if (c < a) { 388 a = c; 389 } 390 return a; 391 } 392 393 401 public static int minimum(int a, int b, int c) { 402 if (b < a) { 403 a = b; 404 } 405 if (c < a) { 406 a = c; 407 } 408 return a; 409 } 410 411 419 public static long maximum(long a, long b, long c) { 420 if (b > a) { 421 a = b; 422 } 423 if (c > a) { 424 a = c; 425 } 426 return a; 427 } 428 429 437 public static int maximum(int a, int b, int c) { 438 if (b > a) { 439 a = b; 440 } 441 if (c > a) { 442 a = c; 443 } 444 return a; 445 } 446 447 449 483 public static int compare(double lhs, double rhs) { 484 if (lhs < rhs) { 485 return -1; 486 } 487 if (lhs > rhs) { 488 return +1; 489 } 490 long lhsBits = Double.doubleToLongBits(lhs); 495 long rhsBits = Double.doubleToLongBits(rhs); 496 if (lhsBits == rhsBits) { 497 return 0; 498 } 499 if (lhsBits < rhsBits) { 506 return -1; 507 } else { 508 return +1; 509 } 510 } 511 512 544 public static int compare(float lhs, float rhs) { 545 if (lhs < rhs) { 546 return -1; 547 } 548 if (lhs > rhs) { 549 return +1; 550 } 551 int lhsBits = Float.floatToIntBits(lhs); 556 int rhsBits = Float.floatToIntBits(rhs); 557 if (lhsBits == rhsBits) { 558 return 0; 559 } 560 if (lhsBits < rhsBits) { 567 return -1; 568 } else { 569 return +1; 570 } 571 } 572 573 575 585 public static boolean isDigits(String str) { 586 if ((str == null) || (str.length() == 0)) { 587 return false; 588 } 589 for (int i = 0; i < str.length(); i++) { 590 if (!Character.isDigit(str.charAt(i))) { 591 return false; 592 } 593 } 594 return true; 595 } 596 597 610 public static boolean isNumber(String str) { 611 if (StringUtils.isEmpty(str)) { 612 return false; 613 } 614 char[] chars = str.toCharArray(); 615 int sz = chars.length; 616 boolean hasExp = false; 617 boolean hasDecPoint = false; 618 boolean allowSigns = false; 619 boolean foundDigit = false; 620 int start = (chars[0] == '-') ? 1 : 0; 622 if (sz > start + 1) { 623 if (chars[start] == '0' && chars[start + 1] == 'x') { 624 int i = start + 2; 625 if (i == sz) { 626 return false; } 628 for (; i < chars.length; i++) { 630 if ((chars[i] < '0' || chars[i] > '9') 631 && (chars[i] < 'a' || chars[i] > 'f') 632 && (chars[i] < 'A' || chars[i] > 'F')) { 633 return false; 634 } 635 } 636 return true; 637 } 638 } 639 sz--; int i = start; 642 while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { 645 if (chars[i] >= '0' && chars[i] <= '9') { 646 foundDigit = true; 647 allowSigns = false; 648 649 } else if (chars[i] == '.') { 650 if (hasDecPoint || hasExp) { 651 return false; 653 } 654 hasDecPoint = true; 655 } else if (chars[i] == 'e' || chars[i] == 'E') { 656 if (hasExp) { 658 return false; 660 } 661 if (!foundDigit) { 662 return false; 663 } 664 hasExp = true; 665 allowSigns = true; 666 } else if (chars[i] == '+' || chars[i] == '-') { 667 if (!allowSigns) { 668 return false; 669 } 670 allowSigns = false; 671 foundDigit = false; } else { 673 return false; 674 } 675 i++; 676 } 677 if (i < chars.length) { 678 if (chars[i] >= '0' && chars[i] <= '9') { 679 return true; 681 } 682 if (chars[i] == 'e' || chars[i] == 'E') { 683 return false; 685 } 686 if (!allowSigns 687 && (chars[i] == 'd' 688 || chars[i] == 'D' 689 || chars[i] == 'f' 690 || chars[i] == 'F')) { 691 return foundDigit; 692 } 693 if (chars[i] == 'l' 694 || chars[i] == 'L') { 695 return foundDigit && !hasExp; 697 } 698 return false; 700 } 701 return !allowSigns && foundDigit; 704 } 705 } 706 | Popular Tags |