public class EasyFormat
extends java.lang.Object
This class provides easy to use formatting methods. They can be particularly useful for setting up tables with fixed width columns or formatting decimal numbers to a fixed number of decimal places. While Java does have some formatting classes (Format, DateFormat, SimpleDataFormat, MessageFormat, NumberFormat, ChoiceFormat, and DecimalFormat) they are not always easy to understand and use especially when setting up tables.
This clsss includes methods for for
When formatting a String, boolean, int, or double to a fixed lenght, the value is right justified by padding with blanks on the left. Values are never chopped if their length is already longer than the specified length. All the methods are static so it is never necessary to create a new EasyFormat object.
The roundTo(number, numDecimalPlaces) function was modeled after the ROUND(number,num_digits) function in Excel. 'num' is the number of decimal places to which x is rounded. If num < 0, then the number is rounded to the left of the decimal point. For example, roundTo(12345.6,-2) = 12300. roundTo returns a double. Occasionally roundTo does not produce the desired result for printed output. This method is intended for situations where internal rounding of numbers is needed in order to continue calculations.
When the only purpose of rounding is to produce a formatted string to be printed, the format(number, numDecimalPlaces) function is a better choice. It works about the same way roundTo works as long as the number of decimal places is greater to or equal to 0.
| Constructor and Description |
|---|
EasyFormat() |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
format(boolean value,
int length)
Returns the boolean value right justified in a string of the specified
length.
|
static java.lang.String |
format(double x,
int numOfDecimals)
Returns a string with the double rounded to
numOfDecimals decimal places.
|
static java.lang.String |
format(double x,
int length,
int numOfDecimals)
Returns a string of the specified length with the double rounded to
numOfDecimals decimal places.
|
static java.lang.String |
format(double x,
java.lang.String pattern)
Formats a double according to the supplied pattern.
|
static java.lang.String |
format(int number,
int length)
Returns the int number right justified in a string of the specified
length.
|
static java.lang.String |
format(java.lang.String str,
int length)
Returns the String str right justified in a string of the specified
length.
|
static void |
main(java.lang.String[] args)
A simple test program.
|
static double |
roundTo(double x,
int num)
Rounds x to num decimal places.
|
public static double roundTo(double x,
int num)
x - - the number to be rounded.num - - the number of decimal places. If num < 0, it rounds to
the number of digits left of the decimalpublic static java.lang.String format(double x,
int numOfDecimals)
Returns "?" if the first parameter is Double.NaN or Double.POSITIVE_INFINITY and "-?" if the parameter is Double.NEGATIVE_INFINITY.
Examples:
format(12.34, -1) = "12"
format(12.34, 0) = "12"
format(12.34, 1) = "12.3"
format(12.34, 5) = "12.34000"
format(-.00012345, 3) = "-.000"
format(12345.111, -2) = "12,345"
format(12345, 2) = "12,345.00"
x - - the number to be rounded.numOfDecimals - - the number of decimal places. If num < 0, it rounds to the
nearest integer.public static java.lang.String format(double x,
int length,
int numOfDecimals)
Returns a string containing "?" if the first parameter is Double.NaN or Double.POSITIVE_INFINITY and "-?" if the parameter is Double.NEGATIVE_INFINITY.
Examples:
format(12.34, 4, -1) = " 12"
format(12.34, 4, 0) = " 12"
format(12.34, 4, 1) = "12.3"
format(12.34, 4, 5) = "12.34000" (the string is longer than specified)
format(-.00012345, 6, 3) = " -.000"
format(12345.111, 8, -2) = " 12,345"
format(12345, 10, 2) = " 12,345.00"
x - - the number to be rounded.numOfDecimals - - the number of decimal places. If num < 0, it rounds to the
nearest integer.public static java.lang.String format(java.lang.String str,
int length)
str - - the String to be formatted.length - - the desired length of the resulting string.public static java.lang.String format(int number,
int length)
number - - the int number to be formatted.length - - the desired length of the resulting string.public static java.lang.String format(boolean value,
int length)
value - - the boolean value to be formatted.length - - the desired length of the resulting string.public static java.lang.String format(double x,
java.lang.String pattern)
x - - the number to be roundedpattern - - the formatting patternpublic static void main(java.lang.String[] args)