All Packages Class Hierarchy This Package Previous Next Index
Class cafeaulait.io.Formatter
java.lang.Object
|
+----cafeaulait.io.Formatter
- public class Formatter
- extends Object
This class provides C-like formatting functions that allow
programmers to convert an integer or floating point number
into a string with a specified, width, precision and format.
For instance this might be used to farmat monetary data to
two decimal places.
Because Java does not have variable length argument lists like C,
a different strategy must be employed. Each number is passed
to a format() method along with formatting instructions.
There are a number of possible things one can do when a nuwber
will not fit inside the specified format. You can throw an exception,
truncate the number, return an error string, or expand the width.
Here I've chosen to expand the width.
The rounding of these precisions still needs work. Currently
excess digits are merely truncated.
-
format(double)
-
This method formats a floating point number to a minimum of 12 characters,
six decimal places, in decimal format,
right aligned, without +
signs on positive numbers.
-
format(double, int)
-
This method formats a floating point number to specified width,
six decimal places, in decimal format,
right aligned, without +
signs on positive numbers.
-
format(double, int, int)
-
This method formats a floating point number to specified width,
number of decimal places, in decimal format,
right aligned, without +
signs on positive numbers.
-
format(double, int, int, boolean)
-
This method formats a floating point number to specified width,
number of decimal places, in exponential or decimal format,
right aligned, without +
signs on positive numbers.
-
format(double, int, int, boolean, boolean)
-
This method formats a floating point number to a specified width,
number of decimal places, in exponential or decimal format,
aligned left or right, without +
signs on positive numbers.
-
format(double, int, int, boolean, boolean, boolean)
-
This method formats a floating point number to a specified width,
number of decimal places, in exponential or decimal format,
aligned left or right, with or without +
signs on positive numbers.
-
format(double, int, int, boolean, boolean, boolean, char)
-
This method formats a floating point number to a specified width,
number of decimal places, in exponential or decimal format,
aligned left or right, with or without +
signs on positive numbers, and with a user-specified padding
character used to fill out the number to the specified width.
-
format(double, String)
-
This method formats a floating point number to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, with a user-specified padding
character used to fill out the number to the specified width,
in a specified base using a C-like formatting string such as
%5f, %15.4F, %-15.3E, %.10f, and so on.
Currently only
f, F, e, and E formats are supported.
-
format(int)
-
This method formats an int in 10 characters
with no leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
-
format(long)
-
This method formats a long in 20 characters
with no leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
-
format(long, int)
-
This method formats an integer to a specified width,
no leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
-
format(long, int, int)
-
This method formats an integer to a specified width,
number of leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
-
format(long, int, int, boolean)
-
This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, without +
signs on positive numbers, filled out to the specified width
with spaces
-
format(long, int, int, boolean, boolean)
-
This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, filled out to the specified width
with spaces
-
format(long, int, int, boolean, boolean, char)
-
This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, with a user-specified padding
character used to fill out the number to the specified width.
-
format(long, int, int, boolean, boolean, char, int)
-
This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, with a user-specified padding
character used to fill out the number to the specified width,
in a specified base
-
format(long, String)
-
This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers,
in octal, decimal, or hexadecimal
using a C-like formatting string such as
%5d, %15.4d, %-15.3x, %.10X, and so on.
-
main(String[])
-
format
public static String format(int i)
- This method formats an int in 10 characters
with no leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
- Parameters:
- d - The number to be formatted
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l)
- This method formats a long in 20 characters
with no leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
- Parameters:
- d - The number to be formatted
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l,
int width)
- This method formats an integer to a specified width,
no leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l,
int width,
int precision)
- This method formats an integer to a specified width,
number of leading zeroes,
right aligned, without +
signs on positive numbers, filled out to the specified width
with spaces
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of leading zeroes
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l,
int width,
int precision,
boolean alignLeft)
- This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, without +
signs on positive numbers, filled out to the specified width
with spaces
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of leading zeroes
- alignLeft - True for left alignment in the width,
false for right alignment
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l,
int width,
int precision,
boolean alignLeft,
boolean usePlus)
- This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, filled out to the specified width
with spaces
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of leading zeroes
- alignLeft - True for left alignment in the width,
false for right alignment
- usePlus - True if positive numbers are prefixed with + signs
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l,
int width,
int precision,
boolean alignLeft,
boolean usePlus,
char pad)
- This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, with a user-specified padding
character used to fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of leading zeroes
- alignLeft - True for left alignment in the width,
false for right alignment
- usePlus - True if positive numbers are prefixed with + signs
- pad - The character used to fill out the number
to the specified width
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l,
int width,
int precision,
boolean alignLeft,
boolean usePlus,
char pad,
int radix)
- This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, with a user-specified padding
character used to fill out the number to the specified width,
in a specified base
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of leading zeroes
- alignLeft - True for left alignment in the width,
false for right alignment
- usePlus - True if positive numbers are prefixed with + signs
- pad - The character used to fill out the number
to the specified width
- radix - The base in which numbers are formatted; e.g. 8, 10, 16
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(double d)
- This method formats a floating point number to a minimum of 12 characters,
six decimal places, in decimal format,
right aligned, without +
signs on positive numbers. Spaces
fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- Returns:
- A String containing a formatted representaion of the number.
format
public static String format(double d,
int width)
- This method formats a floating point number to specified width,
six decimal places, in decimal format,
right aligned, without +
signs on positive numbers. Spaces
fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- Returns:
- A String containing a formatted representaion of the number.
format
public static String format(double d,
int width,
int precision)
- This method formats a floating point number to specified width,
number of decimal places, in decimal format,
right aligned, without +
signs on positive numbers. Spaces
fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of digits after the decimal point
- Returns:
- A String containing a formatted representaion of the number.
format
public static String format(double d,
int width,
int precision,
boolean exponential)
- This method formats a floating point number to specified width,
number of decimal places, in exponential or decimal format,
right aligned, without +
signs on positive numbers. Spaces
fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of digits after the decimal point
- exponential - true if the number is to be displayed in
exponential notation, e.g. 3.50E+09, false otherwise
- Returns:
- A String containing a formatted representaion of the number.
format
public static String format(double d,
int width,
int precision,
boolean exponential,
boolean alignLeft)
- This method formats a floating point number to a specified width,
number of decimal places, in exponential or decimal format,
aligned left or right, without +
signs on positive numbers. Spaces
fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of digits after the decimal point
- exponential - true if the number is to be displayed in
exponential notation, e.g. 3.50E+09, false otherwise
- alignLeft - True for left alignment in the width,
false for right alignment
- Returns:
- A String containing a formatted representaion of the number.
format
public static String format(double d,
int width,
int precision,
boolean exponential,
boolean alignLeft,
boolean usePlus)
- This method formats a floating point number to a specified width,
number of decimal places, in exponential or decimal format,
aligned left or right, with or without +
signs on positive numbers. Spaces
fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of digits after the decimal point
- exponential - true if the number is to be displayed in
exponential notation, e.g. 3.50E+09, false otherwise
- alignLeft - True for left alignment in the width,
false for right alignment
- usePlus - True if positive numbers are prefixed with + signs
- Returns:
- A String containing a formatted representaion of the number.
format
public static String format(double d,
int width,
int precision,
boolean exponential,
boolean alignLeft,
boolean usePlus,
char pad)
- This method formats a floating point number to a specified width,
number of decimal places, in exponential or decimal format,
aligned left or right, with or without +
signs on positive numbers, and with a user-specified padding
character used to fill out the number to the specified width.
- Parameters:
- d - The number to be formatted
- width - The minimum number of characters in the returned string
- precision - The number of digits after the decimal point
- exponential - true if the number is to be displayed in
exponential notation, e.g. 3.50E+09, false otherwise
- alignLeft - True for left alignment in the width,
false for right alignment
- usePlus - True if positive numbers are prefixed with + signs
- pad - The character used to fill out the number
to the specified width
- Returns:
- A String containing a formatted representaion of the number.
format
public static String format(double d,
String s)
- This method formats a floating point number to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers, with a user-specified padding
character used to fill out the number to the specified width,
in a specified base using a C-like formatting string such as
%5f, %15.4F, %-15.3E, %.10f, and so on.
Currently only
f, F, e, and E formats are supported. g and G formats are not supported
- Parameters:
- d - The number to be formatted
- s - A String containing formatting instructions
- Returns:
- A String containing a formatted representaion of the number
format
public static String format(long l,
String s)
- This method formats an integer to a specified width,
number of leading zeroes,
aligned left or right, with or without +
signs on positive numbers,
in octal, decimal, or hexadecimal
using a C-like formatting string such as
%5d, %15.4d, %-15.3x, %.10X, and so on.
- Parameters:
- d - The number to be formatted
- s - A String containing formatting instructions
- Returns:
- A String containing a formatted representaion of the number
main
public static void main(String args[])
All Packages Class Hierarchy This Package Previous Next Index