Java examples for java.lang:Number
Checks if the given number is negative, that is < 0.
//package com.java2s; import java.math.BigDecimal; import java.math.BigInteger; public class Main { public static void main(String[] argv) throws Exception { Number value = new Integer(2); System.out.println(isNegative(value)); }//from w w w . jav a 2s.c om /** * Checks if the given number is negative, that is < 0. * @param value The value to check * @return True is value is not null and its value < 0. */ public static boolean isNegative(Number value) { boolean isNegative = false; if (value != null) { if (BigDecimal.class.isAssignableFrom(value.getClass())) { isNegative = (((BigDecimal) value) .compareTo(BigDecimal.ZERO) < 0); } else if (BigInteger.class.isAssignableFrom(value.getClass())) { isNegative = (((BigInteger) value) .compareTo(BigInteger.ZERO) < 0); } else { isNegative = (value.doubleValue() < 0); } } return isNegative; } /** * Checks if the Class of the given object is assignable from a reference * Class. * @param value The object to check. * @param clazz The reference Class. * @return True if value, it's Class and clazz are not null and value is * assignable from clazz; false otherwise. */ public static boolean isAssignableFrom(Object value, Class clazz) { return isAssignableFrom((value != null) ? value.getClass() : (Class) null, clazz); } /** * Checks if the given Class is assignable from a reference Class. * @param value The Class to check. * @param clazz The reference Class. * @return True if value and clazz are not null and value is assignable from * clazz; false otherwise. */ @SuppressWarnings("unchecked") public static boolean isAssignableFrom(Class value, Class clazz) { return (clazz != null) && (value != null) && clazz.isAssignableFrom(value); // unchecked } }