Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.cantaa.util;
 
 

Author(s):
Hans Lesmeister
 
public class NumberUtil {

   
Converts a Number to (big) Long

Parameters:
number Nmber to convert
Returns:
The Long-Value or null if number is null
   public static Long convertToLong(Number number) {
      if (number == null) {
         return null;
      }
      if (number instanceof Long) {
         return (Longnumber;
      }
      else {
         return Long.valueOf(number.longValue());
      }
   };

   
Converts a (big) Integer to a small int. If the value is null then the defaultvalue is returned

Parameters:
value The Integer to convert
defaultValue Return if value is null
Returns:
int-Value
   public static int convertToInt(Integer valueint defaultValue) {
      if (value == null) {
         return defaultValue;
      }
      else {
         return value.intValue();
      }
   }

   
Converts an Amount to its associated Number (usually a BigDecimal)

Parameters:
amount Amount to convert
Returns:
Number-Representation or null if the passed amount is null
   public static Number convertToNumber(Amount amount) {
      return convertToNumber(amountnull);
   }

   
Converts an Amount to its associated Number (usually a BigDecimal)

Parameters:
amount Amount to convert
defaultValue Value to return if the passed amount is null
Returns:
Number-Representation or the defaultValue if the passed amount is null
   public static Number convertToNumber(Amount amountNumber defaultValue) {
      if (amount == null) {
         return defaultValue;
      }
      else {
         return amount.getValue();
      }
   }

   
Checks if the passed value is a number

Parameters:
value Value to be tested. Can be any type. Tested will be the toString()-result.
Returns:
BigInteger if the value is a number, otherwise null (An Exception is never thrown)
   public static BigDecimal checkNumber(Object value) {
      // Try to make it a string first
      String sval = StringUtil.objectToString(value);
      // If the resulting string is empty then it contains no number
      if (StringUtil.isEmpty(sval)) {
         return null;
      }
      // Try to make a Number out of it
      try {
         // Decimal Comma is replaced by decimal dot so something like 123,34 will work and 123.34 will work as well
         return new BigDecimal(sval.trim().replace(',''.'));
      }
      catch (Exception e) {
         return null;
      }
   }
New to GrepCode? Check out our FAQ X