shh.util
Class Format

Object
  extended byFormat

public class Format
extends Object

Formatting utilities.

Author:
Mark McConnell

Constructor Summary
Format()
           
 
Method Summary
static String cardinal(int x)
          cardinal(58) is "fifty-eight", for example.
static String ordinal(int x)
          ordinal(58) is "fifty-eighth", for example.
static void pad(StringBuffer s, int n)
          Add spaces to s to make its length equal to n.
static String round(float x, int dec_places)
          Given the float x, rounds it off to the nearest dec_places decimal places, if any.
static String roundToSigFig(float x, int sig_figs)
          Converts the number of significant figures (sig_figs) into the appropriate number of decimal places, then passes off the work to round(float, int).
static void setTabStops(int[] value)
          See tab(java.lang.StringBuffer).
static void tab(StringBuffer s)
          Finds the leftmost t in tabStops that's greater than the length of s, and pads s with spaces until its length is t.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Format

public Format()
Method Detail

cardinal

public static String cardinal(int x)
cardinal(58) is "fifty-eight", for example. It would be cool to do this for type long or BigInteger.


ordinal

public static String ordinal(int x)
ordinal(58) is "fifty-eighth", for example. It would be cool to do this for type long or BigInteger.


round

public static String round(float x,
                           int dec_places)
Given the float x, rounds it off to the nearest dec_places decimal places, if any. Examples:

WARNING: there will be errors if rounding x * 10^(dec_places) to the nearest integer overflows the int datatype.

Returns:
A String containing the rounded-off value.
Throws:
IllegalArgumentException - If dec_places is less than 0 (just because we haven't written that case).

roundToSigFig

public static String roundToSigFig(float x,
                                   int sig_figs)
Converts the number of significant figures (sig_figs) into the appropriate number of decimal places, then passes off the work to round(float, int).

The result is incorrect in some ways.


pad

public static void pad(StringBuffer s,
                       int n)
Add spaces to s to make its length equal to n. If s is already of length ≥ n, add one space.


setTabStops

public static void setTabStops(int[] value)
See tab(java.lang.StringBuffer).


tab

public static void tab(StringBuffer s)
Finds the leftmost t in tabStops that's greater than the length of s, and pads s with spaces until its length is t. If no entry in tabStops is big enough, adds one space to s.

The user should reset tabStops using setTabStops(int[]) as desired.