|
ProgramixGenericLib v5.0.1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.programix.math.NumberTools
public class NumberTools
Various tools for working with numbers.
For BigDecimal, see DecimalTools.
| 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 |
|---|
public static final char PAD_CHAR_SPACE
public static final char PAD_CHAR_ZERO
public static final char PAD_CHAR_NONE
public static final char GROUPING_CHAR_COMMA
public static final char GROUPING_CHAR_NONE
| Method Detail |
|---|
public static byte toByte(Number num)
throws RangeValueException
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".
num - source to convert
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(Number)
public static Byte toByteNumber(Number num)
throws RangeValueException
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.
num - source to convert
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.
public static byte toByteForced(Number num)
throws IllegalArgumentException
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.
num - source to convert
IllegalArgumentException - if null is passed intoByte(Number)
public static short toShort(Number num)
throws RangeValueException
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".
num - source to convert
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(Number)
public static Short toShortNumber(Number num)
throws RangeValueException
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.
num - source to convert
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.
public static short toShortForced(Number num)
throws IllegalArgumentException
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.
num - source to convert
IllegalArgumentException - if null is passed intoShort(Number)
public static int toInt(Number num)
throws RangeValueException
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.
num - source to convert
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.toIntForced(Number)
public static Integer toIntegerNumber(Number num)
throws RangeValueException
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.
num - source to convert
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.
public static int toIntForced(Number num)
throws IllegalArgumentException
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.
num - source to convert
IllegalArgumentException - if null is passed intoInt(Number)
public static long toLong(Number num)
throws RangeValueException
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".
num - source to convert
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(Number)
public static Long toLongNumber(Number num)
throws RangeValueException
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.
num - source to convert
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.
public static long toLongForced(Number num)
throws IllegalArgumentException
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.
num - source to convert
IllegalArgumentException - if null is passed intoLong(Number)
public static int parseIntLenient(String source)
throws RangeValueException,
ValueException
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.
source - to be parsed
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.DecimalTools.parseBigDecimal(String),
parseInt(String),
StringTools.winnowDecimal(String)
public static int parseInt(String source,
int defaultValue)
parseIntLenient(String).
If there is trouble parsing, then defaultValue
is returned (no exception is thrown).
public static int parseInt(String source)
parseIntLenient(String).
If there is trouble parsing, then 0
is returned (no exception is thrown).
public static long parseLongLenient(String source)
throws RangeValueException,
ValueException
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.
source - to be parsed
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.DecimalTools.parseBigDecimal(String),
parseLong(String),
StringTools.winnowDecimal(String)
public static long parseLong(String source,
long defaultValue)
parseLongLenient(String).
If there is trouble parsing, then defaultValue
is returned (no exception is thrown).
public static long parseLong(String source)
parseLongLenient(String).
If there is trouble parsing, then 0L
is returned (no exception is thrown).
public static int rangeBound(int minValue,
int value,
int maxValue)
public static int min(int a,
int b,
int c)
public static int max(int a,
int b,
int c)
public static void format(int value,
char padChar,
char groupingChar,
int width,
char[] destination,
int destinationOffset)
throws IllegalArgumentException
PAD_CHAR_NONE, then all slots in
the destination array are overwritten by this method.
value - the value to formatpadChar - 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 charactersdestinationOffset - the position within destination to begin
placing the characters.
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.
public static void format(int value,
char padChar,
int width,
char[] destination,
int destinationOffset)
throws IllegalArgumentException
value - the value to formatpadChar - 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 charactersdestinationOffset - the position within destination to begin
placing the characters.
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.
public static void format(int value,
char padChar,
int width,
char[] destination)
throws IllegalArgumentException
value - the value to formatpadChar - 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
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.
public static void format(int value,
char padChar,
char[] destination)
throws IllegalArgumentException
value - the value to formatpadChar - the character to use as prefix padding if necessary.destination - the location to place the characters
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.
public static String format(int value,
char padChar,
int width)
throws IllegalArgumentException
value - the value to formatpadChar - 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.
IllegalArgumentException - if the width is smaller than the space
needed.
This is a RuntimeException, so callers are not
required to explicitly catch it.
public static String formatComma(int value,
char padChar,
int width)
throws IllegalArgumentException
value - the value to formatpadChar - 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.
IllegalArgumentException - if the width is smaller than the space
needed.
This is a RuntimeException, so callers are not
required to explicitly catch it.
public static String formatPadSpace(int value,
int width)
throws IllegalArgumentException
PAD_CHAR_SPACE (the ' ' character)
is used to prefix the text.
No thousand grouping char is used.
value - the value to formatwidth - the space to fill with the formatted value, prefixing
with the PAD_CHAR_SPACE padding character if necessary.
IllegalArgumentException - if the width is smaller than the space
needed.
This is a RuntimeException, so callers are not
required to explicitly catch it.
public static String formatPadZero(int value,
int width)
throws IllegalArgumentException
PAD_CHAR_ZERO (the '0' character)
is used to prefix the text.
No thousand grouping char is used.
value - the value to formatwidth - the space to fill with the formatted value, prefixing
with the PAD_CHAR_ZERO padding character if necessary.
IllegalArgumentException - if the width is smaller than the space
needed.
This is a RuntimeException, so callers are not
required to explicitly catch it.
public static String formatPadSpace(Integer value,
int width)
throws IllegalArgumentException
PAD_CHAR_SPACE (the ' ' character)
is used to prefix the text.
No thousand grouping char is used.
value - the value to formatwidth - the space to fill with the formatted value, prefixing
with the PAD_CHAR_SPACE padding character if necessary.
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.
public static String formatPadZero(Integer value,
int width)
throws IllegalArgumentException
PAD_CHAR_ZERO (the '0' character)
is used to prefix the text.
No thousand grouping char is used.
value - the value to formatwidth - the space to fill with the formatted value, prefixing
with the PAD_CHAR_ZERO padding character if necessary.
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.
public static StringBuffer format(int value,
char padChar,
int width,
StringBuffer sb)
throws IllegalArgumentException
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.
value - the value to formatpadChar - 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.
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.
public static StringBuffer formatPadSpace(int value,
int width,
StringBuffer sb)
throws IllegalArgumentException
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.
value - the value to formatwidth - 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.
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.
public static StringBuffer formatPadZero(int value,
int width,
StringBuffer sb)
throws IllegalArgumentException
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.
value - the value to formatwidth - 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.
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.public static String formatComma(int value)
public static boolean isEmpty(int[] val)
public static boolean isNotEmpty(int[] val)
public static boolean isEmpty(double[] val)
public static boolean isNotEmpty(double[] val)
public static int sum(int[] val)
val - values to total up.
sum(double[]),
DecimalTools.sum(java.math.BigDecimal[])public static double sum(double[] val)
val - values to total up.
sum(int[]),
DecimalTools.sum(java.math.BigDecimal[])
public static void distributeExcessProportionally(int[] val,
int excessToDistribute)
val - the array to alterexcessToDistribute - the amount to distribute
public static String printArray(byte[] val,
String name)
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.
public static String printArray(int[] val,
String name)
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.
public static String printArray(double[] val,
String name)
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.
|
ProgramixGenericLib v5.0.1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||