ProgramixGenericLib v5.0.1

com.programix.math
Class NumberTools

java.lang.Object
  extended by com.programix.math.NumberTools

public class NumberTools
extends Object

Various tools for working with numbers. For BigDecimal, see DecimalTools.

Author:
Paul Hyde

Field Summary
static char GROUPING_CHAR_COMMA
          The character ',' (comma).
static char GROUPING_CHAR_NONE
          Used to indicate that "thousands" should not be separated by any character at all (no grouping).
static char PAD_CHAR_NONE
          Used to indicate that there should not be any any padding done at all.
static char PAD_CHAR_SPACE
          The character ' ' (space).
static char PAD_CHAR_ZERO
          The character '0' (zero).
 
Method Summary
static void distributeExcessProportionally(int[] val, int excessToDistribute)
          Distributes the excess to the items in the int[] proportional to the amount that each item represents when compared to the total.
static void format(int value, char padChar, char[] destination)
          Formats the specified value into the specified destination.
static void format(int value, char padChar, char groupingChar, int width, char[] destination, int destinationOffset)
          Formats the specified value into the specified destination.
static String format(int value, char padChar, int width)
          Formats the specified value into a string.
static void format(int value, char padChar, int width, char[] destination)
          Formats the specified value into the specified destination.
static void format(int value, char padChar, int width, char[] destination, int destinationOffset)
          Formats the specified value into the specified destination.
static StringBuffer format(int value, char padChar, int width, StringBuffer sb)
          Formats the specified value and appends it to the specified StringBuffer.
static String formatComma(int value)
          Formats the specified value using a comma to groups thousands.
static String formatComma(int value, char padChar, int width)
          Formats the specified value into a string.
static String formatPadSpace(Integer value, int width)
          Formats the specified value into a string.
static String formatPadSpace(int value, int width)
          Formats the specified value into a string.
static StringBuffer formatPadSpace(int value, int width, StringBuffer sb)
          Formats the specified value and appends it to the specified StringBuffer.
static String formatPadZero(Integer value, int width)
          Formats the specified value into a string.
static String formatPadZero(int value, int width)
          Formats the specified value into a string.
static StringBuffer formatPadZero(int value, int width, StringBuffer sb)
          Formats the specified value and appends it to the specified StringBuffer.
static boolean isEmpty(double[] val)
          Returns true if either val is null or val is pointing to a zero-length array.
static boolean isEmpty(int[] val)
          Returns true if either val is null or val is pointing to a zero-length array.
static boolean isNotEmpty(double[] val)
          Returns true if val is not null and val is pointing to an array with a length greater than zero.
static boolean isNotEmpty(int[] val)
          Returns true if val is not null and val is pointing to an array with a length greater than zero.
static int max(int a, int b, int c)
          Returns the largest of the three numbers passed in.
static int min(int a, int b, int c)
          Returns the smallest of the three numbers passed in.
static int parseInt(String source)
          Returns an int derived from parsing the specified source as specified by parseIntLenient(String).
static int parseInt(String source, int defaultValue)
          Returns an int derived from parsing the specified source as specified by parseIntLenient(String).
static int parseIntLenient(String source)
          Permissively parses the passed String into an int.
static long parseLong(String source)
          Returns a long derived from parsing the specified source as specified by parseLongLenient(String).
static long parseLong(String source, long defaultValue)
          Returns a long derived from parsing the specified source as specified by parseLongLenient(String).
static long parseLongLenient(String source)
          Permissively parses the passed String into a long.
static String printArray(byte[] val, String name)
          Returns a String with a dump of the contents of the specified array.
static String printArray(double[] val, String name)
          Returns a String with a dump of the contents of the specified array.
static String printArray(int[] val, String name)
          Returns a String with a dump of the contents of the specified array.
static int rangeBound(int minValue, int value, int maxValue)
          Returns a value that is always at least minValue and at most maxValue.
static double sum(double[] val)
          Returns the sum of all of the values in the array passed in.
static int sum(int[] val)
          Returns the sum of all of the values in the array passed in.
static byte toByte(Number num)
          Returns the value of the Number rounding off to the nearest byte.
static byte toByteForced(Number num)
          Returns the value of the Number rounding off to the nearest byte.
static Byte toByteNumber(Number num)
          Returns the value of the Number rounding off to the nearest Byte.
static int toInt(Number num)
          Returns the value of the Number rounding off to the nearest int.
static Integer toIntegerNumber(Number num)
          Returns the value of the Number rounding off to the nearest Integer.
static int toIntForced(Number num)
          Returns the value of the Number rounding off to the nearest int.
static long toLong(Number num)
          Returns the value of the Number rounding off to the nearest long.
static long toLongForced(Number num)
          Returns the value of the Number rounding off to the nearest long.
static Long toLongNumber(Number num)
          Returns the value of the Number rounding off to the nearest Long.
static short toShort(Number num)
          Returns the value of the Number rounding off to the nearest short.
static short toShortForced(Number num)
          Returns the value of the Number rounding off to the nearest short.
static Short toShortNumber(Number num)
          Returns the value of the Number rounding off to the nearest Short.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAD_CHAR_SPACE

public static final char PAD_CHAR_SPACE
The character ' ' (space). Used here to specify that spaces should be used for any necessary padding. There is nothing special about this char; it is simply defined as a constant for readability.

See Also:
Constant Field Values

PAD_CHAR_ZERO

public static final char PAD_CHAR_ZERO
The character '0' (zero). Used here to specify that zeros should be used for any necessary padding. There is nothing special about this char; it is simply defined as a constant for readability.

See Also:
Constant Field Values

PAD_CHAR_NONE

public static final char PAD_CHAR_NONE
Used to indicate that there should not be any any padding done at all. The value of this char is ''.

See Also:
Constant Field Values

GROUPING_CHAR_COMMA

public static final char GROUPING_CHAR_COMMA
The character ',' (comma). Used here to specify that commas should be used for any necessary thousand's grouping (for example: 12,345). There is nothing special about this char; it is simply defined as a constant for readability.

See Also:
Constant Field Values

GROUPING_CHAR_NONE

public static final char GROUPING_CHAR_NONE
Used to indicate that "thousands" should not be separated by any character at all (no grouping). The value of this char is ''.

See Also:
Constant Field Values
Method Detail

toByte

public static byte toByte(Number num)
                   throws RangeValueException
Returns the value of the Number rounding off to the nearest byte. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up".

Parameters:
num - source to convert
Returns:
the value as a byte
Throws:
RangeValueException - if the value is not in the valid range of values for byte. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.
See Also:
toByteForced(Number)

toByteNumber

public static Byte toByteNumber(Number num)
                         throws RangeValueException
Returns the value of the Number rounding off to the nearest Byte. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up". If a Byte is passed in, it is simply (and efficiently) returned.

Parameters:
num - source to convert
Returns:
the value as a Byte
Throws:
RangeValueException - if the value is not in the valid range of values for Byte. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.

toByteForced

public static byte toByteForced(Number num)
                         throws IllegalArgumentException
Returns the value of the Number rounding off to the nearest byte. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up" rule.

This method never throws RangeValueException. Instead, if the rounded off number is less than Byte.MIN_VALUE, then Byte.MIN_VALUE is returned. Likewise, if the rounded off number is greater than Byte.MAX_VALUE, then Byte.MAX_VALUE is returned.

Parameters:
num - source to convert
Returns:
the value as an byte
Throws:
IllegalArgumentException - if null is passed in
See Also:
toByte(Number)

toShort

public static short toShort(Number num)
                     throws RangeValueException
Returns the value of the Number rounding off to the nearest short. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up".

Parameters:
num - source to convert
Returns:
the value as a short
Throws:
RangeValueException - if the value is not in the valid range of values for short. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.
See Also:
toShortForced(Number)

toShortNumber

public static Short toShortNumber(Number num)
                           throws RangeValueException
Returns the value of the Number rounding off to the nearest Short. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up". If a Short is passed in, it is simply (and efficiently) returned.

Parameters:
num - source to convert
Returns:
the value as a Short
Throws:
RangeValueException - if the value is not in the valid range of values for Short. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.

toShortForced

public static short toShortForced(Number num)
                           throws IllegalArgumentException
Returns the value of the Number rounding off to the nearest short. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up" rule.

This method never throws RangeValueException. Instead, if the rounded off number is less than Short.MIN_VALUE, then Short.MIN_VALUE is returned. Likewise, if the rounded off number is greater than Short.MAX_VALUE, then Short.MAX_VALUE is returned.

Parameters:
num - source to convert
Returns:
the value as an short
Throws:
IllegalArgumentException - if null is passed in
See Also:
toShort(Number)

toInt

public static int toInt(Number num)
                 throws RangeValueException
Returns the value of the Number rounding off to the nearest int. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up" rule.

Parameters:
num - source to convert
Returns:
the value as an int
Throws:
RangeValueException - if the value is not in the valid range of values for int. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.
See Also:
toIntForced(Number)

toIntegerNumber

public static Integer toIntegerNumber(Number num)
                               throws RangeValueException
Returns the value of the Number rounding off to the nearest Integer. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up". If a Integer is passed in, it is simply (and efficiently) returned.

Parameters:
num - source to convert
Returns:
the value as a Integer
Throws:
RangeValueException - if the value is not in the valid range of values for Integer. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.

toIntForced

public static int toIntForced(Number num)
                       throws IllegalArgumentException
Returns the value of the Number rounding off to the nearest int. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up" rule.

This method never throws RangeValueException. Instead, if the rounded off number is less than Integer.MIN_VALUE, then Integer.MIN_VALUE is returned. Likewise, if the rounded off number is greater than Integer.MAX_VALUE, then Integer.MAX_VALUE is returned.

Parameters:
num - source to convert
Returns:
the value as an int
Throws:
IllegalArgumentException - if null is passed in
See Also:
toInt(Number)

toLong

public static long toLong(Number num)
                   throws RangeValueException
Returns the value of the Number rounding off to the nearest long. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up".

Parameters:
num - source to convert
Returns:
the value as a long
Throws:
RangeValueException - if the value is not in the valid range of values for long. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.
See Also:
toLongForced(Number)

toLongNumber

public static Long toLongNumber(Number num)
                         throws RangeValueException
Returns the value of the Number rounding off to the nearest Long. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up". If a Long is passed in, it is simply (and efficiently) returned.

Parameters:
num - source to convert
Returns:
the value as a Long
Throws:
RangeValueException - if the value is not in the valid range of values for Long. RangeValueException is a subclass of ValueException which is a RuntimeException and is therefore not required to be caught.

toLongForced

public static long toLongForced(Number num)
                         throws IllegalArgumentException
Returns the value of the Number rounding off to the nearest long. Double, Float, and BigDecimal are fine to pass in, the value is rounded off using the widely used "round half up" rule.

This method never throws RangeValueException. Instead, if the rounded off number is less than Long.MIN_VALUE, then Long.MIN_VALUE is returned. Likewise, if the rounded off number is greater than Long.MAX_VALUE, then Long.MAX_VALUE is returned.

Parameters:
num - source to convert
Returns:
the value as an long
Throws:
IllegalArgumentException - if null is passed in
See Also:
toLong(Number)

parseIntLenient

public static int parseIntLenient(String source)
                           throws RangeValueException,
                                  ValueException
Permissively parses the passed String into an int. If a decimal point is present, then the source is attempted to be interpretted as BigDecimal and then rounded off to the nearest int. Parses the passed string by ignoring all characters not in the set {0, 1, 2 ,3 , 4, 5, 6, 7, 8, 9, -}. The negative sign '-' is ignored if it is not the first valid character; and only the first decimal point '.' is kept, any additional decimal points are ignored. There is no need to trim any whitespace before calling this method as none of the whitespace characters are in the valid set of characters listed above.

If null, a zero-length String, a String containing only whitespace, or a String that contains no valid characters is passed in, then a ValueException is thrown. If the resulting value is not in the range of int, then a RangeValueException is thrown (which is a subclass of ValueException). Both are RuntimeException's and are therefore not required to be caught.

Parameters:
source - to be parsed
Returns:
the resulting int.
Throws:
RangeValueException - if the value does not fit into the value range for an int. RangeValueException is a RuntimeException and is not required to be caught (it is also a subclass of ValueException).
ValueException - if the parsing fails. ValueException is a RuntimeException and is not required to be caught.
See Also:
DecimalTools.parseBigDecimal(String), parseInt(String), StringTools.winnowDecimal(String)

parseInt

public static int parseInt(String source,
                           int defaultValue)
Returns an int derived from parsing the specified source as specified by parseIntLenient(String). If there is trouble parsing, then defaultValue is returned (no exception is thrown).


parseInt

public static int parseInt(String source)
Returns an int derived from parsing the specified source as specified by parseIntLenient(String). If there is trouble parsing, then 0 is returned (no exception is thrown).


parseLongLenient

public static long parseLongLenient(String source)
                             throws RangeValueException,
                                    ValueException
Permissively parses the passed String into a long. If a decimal point is present, then the source is attempted to be interpretted as BigDecimal and then rounded off to the nearest long. Parses the passed string by ignoring all characters not in the set {0, 1, 2 ,3 , 4, 5, 6, 7, 8, 9, -}. The negative sign '-' is ignored if it is not the first valid character; and only the first decimal point '.' is kept, any additional decimal points are ignored. There is no need to trim any whitespace before calling this method as none of the whitespace characters are in the valid set of characters listed above.

If null, a zero-length String, a String containing only whitespace, or a String that contains no valid characters is passed in, then a ValueException is thrown. If the resulting value is not in the range of long, then a RangeValueException is thrown (which is a subclass of ValueException). Both are RuntimeException's and are therefore not required to be caught.

Parameters:
source - to be parsed
Returns:
the resulting long.
Throws:
RangeValueException - if the value does not fit into the value range for an long. RangeValueException is a RuntimeException and is not required to be caught (it is also a subclass of ValueException).
ValueException - if the parsing fails. ValueException is a RuntimeException and is not required to be caught.
See Also:
DecimalTools.parseBigDecimal(String), parseLong(String), StringTools.winnowDecimal(String)

parseLong

public static long parseLong(String source,
                             long defaultValue)
Returns a long derived from parsing the specified source as specified by parseLongLenient(String). If there is trouble parsing, then defaultValue is returned (no exception is thrown).


parseLong

public static long parseLong(String source)
Returns a long derived from parsing the specified source as specified by parseLongLenient(String). If there is trouble parsing, then 0L is returned (no exception is thrown).


rangeBound

public static int rangeBound(int minValue,
                             int value,
                             int maxValue)
Returns a value that is always at least minValue and at most maxValue. If value is less than minValue, then minValue is returned. If value is greater than maxValue, then maxValue is returned. If maxValue is (very strangely) less than minValue, then maxValue will be returned.


min

public static int min(int a,
                      int b,
                      int c)
Returns the smallest of the three numbers passed in.


max

public static int max(int a,
                      int b,
                      int c)
Returns the largest of the three numbers passed in.


format

public static void format(int value,
                          char padChar,
                          char groupingChar,
                          int width,
                          char[] destination,
                          int destinationOffset)
                   throws IllegalArgumentException
Formats the specified value into the specified destination. The text version of the value is right-justified in the destination and (if necessary) the padChar is used to prefix the text to fully fill the destination. If padChar is not PAD_CHAR_NONE, then all slots in the destination array are overwritten by this method.

Parameters:
value - the value to format
padChar - the character to use as prefix padding if necessary.
groupingChar - the character to use to group thousands if necessary.
width - the space to fill with the formatted value, prefixing with the specified padding character if necessary.
destination - the location to place the characters
destinationOffset - the position within destination to begin placing the characters.
Throws:
IllegalArgumentException - if the destination char[] is too small to hold the value; if the width is larger than the space available; and if the offset is out-of-range. This is a RuntimeException, so callers are not required to explicitly catch it.

format

public static void format(int value,
                          char padChar,
                          int width,
                          char[] destination,
                          int destinationOffset)
                   throws IllegalArgumentException
Formats the specified value into the specified destination. The text version of the value is right-justified in the destination and (if necessary) the padChar is used to prefix the text to fully fill the destination. All slots in the destination array are overwritten by this method. No thousand grouping char is used.

Parameters:
value - the value to format
padChar - the character to use as prefix padding if necessary.
width - the space to fill with the formatted value, prefixing with the specified padding character if necessary.
destination - the location to place the characters
destinationOffset - the position within destination to begin placing the characters.
Throws:
IllegalArgumentException - if the destination char[] is too small to hold the value; if the width is larger than the space available; and if the offset is out-of-range. This is a RuntimeException, so callers are not required to explicitly catch it.

format

public static void format(int value,
                          char padChar,
                          int width,
                          char[] destination)
                   throws IllegalArgumentException
Formats the specified value into the specified destination. The text version of the value is right-justified width characters into the destination and (if necessary) the padChar is used to prefix the text. All the slots from index 0 to width - 1 in the destination array are overwritten by this method. No thousand grouping char is used.

Parameters:
value - the value to format
padChar - the character to use as prefix padding if necessary.
width - the space to fill with the formatted value, prefixing with the specified padding character if necessary.
destination - the location to place the characters
Throws:
IllegalArgumentException - if the destination char[] is too small to hold the value; if the width is larger than the space available; and if the offset is out-of-range. This is a RuntimeException, so callers are not required to explicitly catch it.

format

public static void format(int value,
                          char padChar,
                          char[] destination)
                   throws IllegalArgumentException
Formats the specified value into the specified destination. The text version of the value is right-justified in the destination and (if necessary) the padChar is used to prefix the text to fully fill the destination. All slots in the destination array are overwritten by this method. No thousand grouping char is used.

Parameters:
value - the value to format
padChar - the character to use as prefix padding if necessary.
destination - the location to place the characters
Throws:
IllegalArgumentException - if the destination char[] is too small to hold the value. This is a RuntimeException, so callers are not required to explicitly catch it.

format

public static String format(int value,
                            char padChar,
                            int width)
                     throws IllegalArgumentException
Formats the specified value into a string. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary the padChar is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
padChar - the character to use as prefix padding if necessary.
width - the space to fill with the formatted value, prefixing with the specified padding character if necessary.
Throws:
IllegalArgumentException - if the width is smaller than the space needed. This is a RuntimeException, so callers are not required to explicitly catch it.

formatComma

public static String formatComma(int value,
                                 char padChar,
                                 int width)
                          throws IllegalArgumentException
Formats the specified value into a string. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary the padChar is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
padChar - the character to use as prefix padding if necessary.
width - the space to fill with the formatted value, prefixing with the specified padding character if necessary.
Throws:
IllegalArgumentException - if the width is smaller than the space needed. This is a RuntimeException, so callers are not required to explicitly catch it.

formatPadSpace

public static String formatPadSpace(int value,
                                    int width)
                             throws IllegalArgumentException
Formats the specified value into a string. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary PAD_CHAR_SPACE (the ' ' character) is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
width - the space to fill with the formatted value, prefixing with the PAD_CHAR_SPACE padding character if necessary.
Throws:
IllegalArgumentException - if the width is smaller than the space needed. This is a RuntimeException, so callers are not required to explicitly catch it.

formatPadZero

public static String formatPadZero(int value,
                                   int width)
                            throws IllegalArgumentException
Formats the specified value into a string. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary PAD_CHAR_ZERO (the '0' character) is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
width - the space to fill with the formatted value, prefixing with the PAD_CHAR_ZERO padding character if necessary.
Throws:
IllegalArgumentException - if the width is smaller than the space needed. This is a RuntimeException, so callers are not required to explicitly catch it.

formatPadSpace

public static String formatPadSpace(Integer value,
                                    int width)
                             throws IllegalArgumentException
Formats the specified value into a string. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary PAD_CHAR_SPACE (the ' ' character) is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
width - the space to fill with the formatted value, prefixing with the PAD_CHAR_SPACE padding character if necessary.
Throws:
IllegalArgumentException - if the width is smaller than the space needed or if the passed Integer reference is null. This is a RuntimeException, so callers are not required to explicitly catch it.

formatPadZero

public static String formatPadZero(Integer value,
                                   int width)
                            throws IllegalArgumentException
Formats the specified value into a string. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary PAD_CHAR_ZERO (the '0' character) is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
width - the space to fill with the formatted value, prefixing with the PAD_CHAR_ZERO padding character if necessary.
Throws:
IllegalArgumentException - if the width is smaller than the space needed or if the passed Integer reference is null. This is a RuntimeException, so callers are not required to explicitly catch it.

format

public static StringBuffer format(int value,
                                  char padChar,
                                  int width,
                                  StringBuffer sb)
                           throws IllegalArgumentException
Formats the specified value and appends it to the specified StringBuffer. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary the padChar is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
padChar - the character to use as prefix padding if necessary.
width - the space to fill with the formatted value, prefixing with the specified padding character if necessary.
sb - the destination for the formatted value.
Returns:
the same StringBuffer that was passed in (for ease of method chaining if desired).
Throws:
IllegalArgumentException - if the width is smaller than the space needed or if sb is null. This is a RuntimeException, so callers are not required to explicitly catch it.

formatPadSpace

public static StringBuffer formatPadSpace(int value,
                                          int width,
                                          StringBuffer sb)
                                   throws IllegalArgumentException
Formats the specified value and appends it to the specified StringBuffer. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary PAD_CHAR_SPACE (the ' ' character) is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
width - the space to fill with the formatted value, prefixing with the PAD_CHAR_SPACE padding character if necessary.
sb - the destination for the formatted value.
Returns:
the same StringBuffer that was passed in (for ease of method chaining if desired).
Throws:
IllegalArgumentException - if the width is smaller than the space needed or if sb is null. This is a RuntimeException, so callers are not required to explicitly catch it.

formatPadZero

public static StringBuffer formatPadZero(int value,
                                         int width,
                                         StringBuffer sb)
                                  throws IllegalArgumentException
Formats the specified value and appends it to the specified StringBuffer. The text version of the value is right-justified in the generated string which is exactly width characters long. If necessary PAD_CHAR_ZERO (the '0' character) is used to prefix the text. No thousand grouping char is used.

Parameters:
value - the value to format
width - the space to fill with the formatted value, prefixing with the PAD_CHAR_ZERO padding character if necessary.
sb - the destination for the formatted value.
Returns:
the same StringBuffer that was passed in (for ease of method chaining if desired).
Throws:
IllegalArgumentException - if the width is smaller than the space needed or if sb is null. This is a RuntimeException, so callers are not required to explicitly catch it.

formatComma

public static String formatComma(int value)
Formats the specified value using a comma to groups thousands. All leading and trailing whitespace is trimmed.


isEmpty

public static boolean isEmpty(int[] val)
Returns true if either val is null or val is pointing to a zero-length array.


isNotEmpty

public static boolean isNotEmpty(int[] val)
Returns true if val is not null and val is pointing to an array with a length greater than zero.


isEmpty

public static boolean isEmpty(double[] val)
Returns true if either val is null or val is pointing to a zero-length array.


isNotEmpty

public static boolean isNotEmpty(double[] val)
Returns true if val is not null and val is pointing to an array with a length greater than zero.


sum

public static int sum(int[] val)
Returns the sum of all of the values in the array passed in.

Parameters:
val - values to total up.
Returns:
the sum of all the values. Zero is returned if val is empty (null or zero-length).
See Also:
sum(double[]), DecimalTools.sum(java.math.BigDecimal[])

sum

public static double sum(double[] val)
Returns the sum of all of the values in the array passed in.

Parameters:
val - values to total up.
Returns:
the sum of all the values. Zero is returned if val is empty (null or zero-length).
See Also:
sum(int[]), DecimalTools.sum(java.math.BigDecimal[])

distributeExcessProportionally

public static void distributeExcessProportionally(int[] val,
                                                  int excessToDistribute)
Distributes the excess to the items in the int[] proportional to the amount that each item represents when compared to the total. If the excess is a negative number, the values will be proportionally shrunk.

Parameters:
val - the array to alter
excessToDistribute - the amount to distribute

printArray

public static String printArray(byte[] val,
                                String name)
Returns a String with a dump of the contents of the specified array. The output uses the passed name when referring to the array.

Parameters:
val - the array for format. OK to pass null and OK to pass a zero-length array (there will be some output).
name - the name of the array to use in the output.
Returns:
a String with the formatted array contents.

printArray

public static String printArray(int[] val,
                                String name)
Returns a String with a dump of the contents of the specified array. The output uses the passed name when referring to the array.

Parameters:
val - the array for format. OK to pass null and OK to pass a zero-length array (there will be some output).
name - the name of the array to use in the output.
Returns:
a String with the formatted array contents.

printArray

public static String printArray(double[] val,
                                String name)
Returns a String with a dump of the contents of the specified array. The output uses the passed name when referring to the array.

Parameters:
val - the array for format. OK to pass null and OK to pass a zero-length array (there will be some output).
name - the name of the array to use in the output.
Returns:
a String with the formatted array contents.

ProgramixGenericLib v5.0.1

Copyright © 2001-2009 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.