com.informagen.F

Achieving C printf like formatting in Java

F folder
Installer which creates a folder containing a Metrowerks project, source code and JBinary application.

F.java
Class source code

Ftest.java
Text based application used during development to test F class.

Ftest.output
Results of running Ftest.

Ftest
A runnable JBinary application.


Usage:

It is apparently impossible to declare a Java method that accepts variable numbers of any type of argument. You can declare it to take Objects, but numeric variables and constants are not, in fact, Objects.

However, using the built-in string concatenation, it's almost as convenient to make a series of single-argument formatting routines.

Class F can format the following datatypes: boolean, byte, char, short, int, long, float, double, Boolean, Byte, Character, Short, Integer, Long, Float, Date, Color, StringBuffer, String, and Object


For each data type there is a set of overloaded methods, each returning a formatted String. There's the plain formatting version:
F.f(x)

There's a version specifying a field width (int):
F.f(x, width)

There's a version which takes formatting flags (short):
F.f(x, flags)

And there's a version that takes both:
F.f(x, width, flags)

Currently available flags are:
F.RJ - right justify (the default)
F.LJ - left justify
F.CJ - center justify
F.ZF - zero fill
F.RF - repeat fill
F.HX - hexadecimal
F.OC - octal
F.BN - binary
F.LC - lowercase
F.UC - uppercase
F.XC - OK to exceed field width
F.TR - truncate string to width

Flags can be combined with the "|" operator, but must then be cast to a short as bitwise OR operations in Java return an int.

Right justify is the default for all formatting.

Center justify must be used with a width and will not work with zero or repeat fill or left justify. In these cases it is ignored.

The HX, OC and BN flags produce unsigned output. Hexidecimal representation have an "0x" prefix and octal representation have a "0" prefix.

Repeat fill is useful for making string of the same character

Uppercase and lowecase can be used on numbers which contain letter ie Hexadecimal or exponential.

Use XC when the field width isn't equal to zero ie "DEFAULT" but you don't might if the value exceed its field boundaries.

Use TR with care with numeric fields as the output could be misinterpreted. XC will override TR.


Dates:

For formatting dates there are a set of predefined format strings for convienence. See the documentation for the class "java.text.SimpleDateFormat" for an extensive description of the format used in date templates.
F.SYBASEDATE = "MMM dd yyyy hh:mm:ss:SSSaa" Sybase datetime stamp
F.DATE = "dd-MMM-yyyy" Three character month
F.TIME = "hh:mmaa" AM/PM 12 hour time
F.MILTIME = "HHmm" 24 hour time ie Military
F.MONTH = "MMMM" Full month name
F.WEEKDAY = "EEEE" Full weekday name


Floats and Doubles:

For real numbers, ie doubles and floats, there's a decimalDigits (int)parameter.
F.f(d, width, decimalDigits)

If the value of decimalDigits is negative the value will be treated as the number of significant figures to be printed. Significant figures are used in scientific calculations in order to express intermediate and final calculation with the proper significance from the measured values.