util
Class Num2Str

java.lang.Object
  extended by util.Num2Str

public class Num2Str
extends java.lang.Object

a simple class to format numbers to a string in a way that I like--limited significant digits, scientific notation if they are too large or small. Decimal Format is just too inflexible

Author:
Daniel Wachsstock

Field Summary
static double LOG10
          the natural logarithm of 10, for calculating log base 10
static java.lang.String MAX_STRING
          a String that is larger than anything produced by format(double) using the default number of digits.
 
Constructor Summary
Num2Str()
          Creates a new instance of Num2Str with 3 significant digits after the decimal
Num2Str(int numDigits)
          Creates a new instance of Num2Str with a set number of significant digits.
 
Method Summary
 java.lang.StringBuffer format(double d)
          format a number.
static java.lang.String formatNumber(double d)
          static method that uses the default number of digits
static java.lang.String formatNumber(java.lang.Object value)
          static method that uses the default number of digits
 java.lang.String maxString()
          returns a string that is larger than any number that will be produced.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG10

public static final double LOG10
the natural logarithm of 10, for calculating log base 10


MAX_STRING

public static final java.lang.String MAX_STRING
a String that is larger than anything produced by format(double) using the default number of digits.

See Also:
maxString(), Constant Field Values
Constructor Detail

Num2Str

public Num2Str()
Creates a new instance of Num2Str with 3 significant digits after the decimal


Num2Str

public Num2Str(int numDigits)
Creates a new instance of Num2Str with a set number of significant digits.

Parameters:
numDigits - the number of significant digits after the decimal (numDigits + 1) total
Throws:
java.lang.IllegalArgumentException - if numDigits <= 0
Method Detail

format

public java.lang.StringBuffer format(double d)
format a number. For numbers with absolute value less than 0.1, use scientific notation. For numbers > 10^numdigits, use scientific notation For numbers in between, use fixed notation


maxString

public java.lang.String maxString()
returns a string that is larger than any number that will be produced. Useful for planning formatting

Returns:
a string that will take up no less space than any number produced by format(double) Assumes that "#" is larger than any digit.

formatNumber

public static java.lang.String formatNumber(double d)
static method that uses the default number of digits

Parameters:
d - the number to format
Returns:
a String representation of the number
See Also:
for the details of formatting

formatNumber

public static java.lang.String formatNumber(java.lang.Object value)
static method that uses the default number of digits

Parameters:
value - an Object (that must be a Number) to format
Returns:
a String representation of the number
Throws:
java.lang.ClassCastException - if value is not a Number
See Also:
for the details of formatting