Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.hartmath.lib
Class Formatter  view Formatter download Formatter.java

java.lang.Object
  extended bycom.hartmath.lib.Formatter

public class Formatter
extends java.lang.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 format 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. The format() method returns a formatted string which may then be passed to System.out.println() or other methods. In short this is more similar to C's sprintf() than to printf().

There are a number of possible things one can do when a number 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.


Constructor Summary
private Formatter()
           
 
Method Summary
static java.lang.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.
static java.lang.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.
static java.lang.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.
static java.lang.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.
static java.lang.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.
static java.lang.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.
static java.lang.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.
static java.lang.String format(double d, java.lang.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.
static java.lang.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
static java.lang.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
static java.lang.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
static java.lang.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
static java.lang.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
static java.lang.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
static java.lang.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.
static java.lang.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
static java.lang.String format(long l, java.lang.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.
static void main(java.lang.String[] args)
           
private static void test()
           
private static void testFloatingPoint(double x)
           
private static void testInteger(long n)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Formatter

private Formatter()
Method Detail

format

public static java.lang.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


format

public static java.lang.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


format

public static java.lang.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


format

public static java.lang.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


format

public static java.lang.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


format

public static java.lang.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


format

public static java.lang.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.


format

public static java.lang.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


format

public static java.lang.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.


format

public static java.lang.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.


format

public static java.lang.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.


format

public static java.lang.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.


format

public static java.lang.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.


format

public static java.lang.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.


format

public static java.lang.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.


format

public static java.lang.String format(double d,
                                      java.lang.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


format

public static java.lang.String format(long l,
                                      java.lang.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.


main

public static void main(java.lang.String[] args)

test

private static void test()

testInteger

private static void testInteger(long n)

testFloatingPoint

private static void testFloatingPoint(double x)