Check if number are in a given range : Number « Data Type « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Data Type » NumberScreenshots 
Check if number are in a given range
   
/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * Check if number are in a given range 
 
 @author Cedric Chabanois (cchabanois at gmail.com)
 *
 */
public class NumberInRange {

  public static final BigInteger BYTE_MIN = BigInteger
      .valueOf((longByte.MIN_VALUE);
  public static final BigInteger BYTE_MAX = BigInteger
      .valueOf((longByte.MAX_VALUE);
  public static final BigInteger SHORT_MIN = BigInteger
      .valueOf((longShort.MIN_VALUE);
  public static final BigInteger SHORT_MAX = BigInteger
      .valueOf((longShort.MAX_VALUE);
  public static final BigInteger INTEGER_MIN = BigInteger
      .valueOf((longInteger.MIN_VALUE);
  public static final BigInteger INTEGER_MAX = BigInteger
      .valueOf((longInteger.MAX_VALUE);
  public static final BigInteger LONG_MIN = BigInteger
      .valueOf(Long.MIN_VALUE);
  public static final BigInteger LONG_MAX = BigInteger
      .valueOf(Long.MAX_VALUE);

  public static final BigDecimal FLOAT_MAX = new BigDecimal(Float.MAX_VALUE);
  public static final BigDecimal FLOAT_MIN = new BigDecimal(-Float.MAX_VALUE);
  public static final BigDecimal DOUBLE_MAX = new BigDecimal(Double.MAX_VALUE);
  public static final BigDecimal DOUBLE_MIN = new BigDecimal(
      -Double.MAX_VALUE);

  public static boolean isInByteRange(Number number) {
    return isInRange(number, BYTE_MIN, BYTE_MAX);
  }

  public static boolean isInShortRange(Number number) {
    return isInRange(number, SHORT_MIN, SHORT_MAX);
  }

  public static boolean isInIntegerRange(Number number) {
    return isInRange(number, INTEGER_MIN, INTEGER_MAX);
  }

  public static boolean isInLongRange(Number number) {
    return isInRange(number, LONG_MIN, LONG_MAX);
  }

  public static boolean isInRange(Number number, BigInteger min,
      BigInteger max) {
    try {
      BigInteger bigInteger = null;
      if (number instanceof Byte || number instanceof Short
          || number instanceof Integer || number instanceof Long) {
        bigInteger = BigInteger.valueOf(number.longValue());
      else if (number instanceof Float || number instanceof Double) {
        bigInteger = new BigDecimal(number.doubleValue())
            .toBigInteger();
      else if (number instanceof BigInteger) {
        bigInteger = (BigIntegernumber;
      else if (number instanceof BigDecimal) {
        bigInteger = ((BigDecimalnumber).toBigInteger();
      else {
        // not a standard number
        bigInteger = new BigDecimal(number.doubleValue())
            .toBigInteger();
      }
      return max.compareTo(bigInteger>= 0
          && min.compareTo(bigInteger<= 0;
    catch (NumberFormatException e) {
      return false;
    }
  }

  public static boolean isInRange(Number number, BigDecimal min,
      BigDecimal max) {
    try {
      BigDecimal bigDecimal = null;
      if (number instanceof Byte || number instanceof Short
          || number instanceof Integer || number instanceof Long) {
        bigDecimal = new BigDecimal(number.longValue());
      else if (number instanceof Float || number instanceof Double) {
        bigDecimal = new BigDecimal(number.doubleValue());
      else if (number instanceof BigInteger) {
        bigDecimal = new BigDecimal((BigIntegernumber);
      else if (number instanceof BigDecimal) {
        bigDecimal = (BigDecimalnumber;
      else {
        bigDecimal = new BigDecimal(number.doubleValue());
      }
      return max.compareTo(bigDecimal>= 0
          && min.compareTo(bigDecimal<= 0;
    catch (NumberFormatException e) {
      return false;
    }
  }

  public static boolean isInFloatRange(Number number) {
    return isInRange(number, FLOAT_MIN, FLOAT_MAX);
  }

  public static boolean isInDoubleRange(Number number) {
    return isInRange(number, DOUBLE_MIN, DOUBLE_MAX);
  }

}

   
    
    
  
Related examples in the same category
1. Methods for number conversion and parsing
2. Get Digit Number String
3. Convert to numbers
4. Check Number properties and convert from Number
5. Various number-related routines and classes that are frequently used
6. Turns a string value into a java.lang.Number.
7. Provides IEEE-754r variants of NumberUtils methods.
8. Checks whether the String a valid Java number.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.