public class NumberFormatter
extends java.lang.Object
System.out.println(new NumberFormatter("+##.###").format(3.1119));
--> +03.112
System.out.println(NumberFormatter.ISOtime());
--> 2006-01-17 T 21:01:00.000
System.out.println(NumberFormatter.ISOweekTime());
--> 2006-w03-2 T 21:01:00.000
The pattern for numbers may look like
[+|-|+/-] #*[x]#* [.#*]
without blanks. 'x' is a separator character for groups of 3 digits. Only one
x will be recognized and it will always separate groups of 3, no matter where it
appears. Examples:
+###.#####
-#
+/-#.######
##
+#'###'###.##
If the formatted number is negative, a negative sign is always output. If the number
is positive, a sign will only be output if the pattern starts with "+" or "+/-".
The output will contain at least as many digits as specified in the pattern.
If the integer part of a number contains more digits than specified in the pattern,
the digits will be output nevertheless.
If the fractional part of a number contains more digits than specified in the pattern,
the number will be rounded and the digits will not be output.
Instead of '#', you may write digits.| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
append
What shall be appended
|
static NumberFormatter |
fiveDigits
Predefined NumberFormatter for #####
|
protected java.util.regex.Pattern |
FORMAT
Pattern for the pattern string
|
static NumberFormatter |
fourDigits
Predefined NumberFormatter for ####
|
static NumberFormatter |
fourFractions
Predefined NumberFormatter for #.####
|
protected int |
fractions
Holds the number of fraction digits
|
protected int |
integers
Holds the number of integer digits
|
static NumberFormatter |
oneDigit
Predefined NumberFormatter for #
|
protected char |
separator
Separates groups of 3 digits
|
protected boolean |
showPosSign
TRUE if positive sign shall be displayed
|
static NumberFormatter |
sixDigits
Predefined NumberFormatter for ######
|
static NumberFormatter |
threeDigits
Predefined NumberFormatter for ###
|
static NumberFormatter |
twoDigits
Predefined NumberFormatter for ##
|
static NumberFormatter |
twoFractions
Predefined NumberFormatter for #.##
|
| Constructor and Description |
|---|
NumberFormatter(java.lang.String f)
Creates a NumberFormatter for a pattern
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
format(double d)
Converts a double to a String
|
static java.lang.String |
formatMS(long ms)
Converts milliseconds to a nice String
|
static java.lang.String |
ISOdate()
Returns an ISO8601 string representation of the current date
|
static java.lang.String |
ISOdate(java.util.Calendar c)
Returns an ISO8601 string representation of the time given
by the calendar
|
static java.lang.String |
ISOtime()
Returns an ISO8601 string representation of the current time
|
static java.lang.String |
ISOtime(java.util.Calendar c)
Returns an ISO8601 string representation of the time given
by the calendar
|
static java.lang.String |
ISOweekTime()
Returns an ISO8601 string representation of the current time,
using a week-oriented representation
|
static java.lang.String |
ISOweekTime(java.util.Calendar c)
Returns an ISO8601 string representation of the time given
by the calendar, using a week-oriented representation
|
static void |
main(java.lang.String[] argv)
Test routine
|
static java.lang.String |
timeStamp()
Returns a current timestamp
|
static java.lang.String |
timeStamp(java.util.Calendar c)
Returns a timestamp from a Calendar
|
static java.lang.String |
timeStamp(java.lang.String isoTime)
Returns a timestamp from an ISOweekTime or an ISOtime
|
protected int integers
protected int fractions
protected boolean showPosSign
protected char separator
protected java.util.regex.Pattern FORMAT
protected java.lang.String append
public static NumberFormatter sixDigits
public static NumberFormatter fiveDigits
public static NumberFormatter fourDigits
public static NumberFormatter threeDigits
public static NumberFormatter twoDigits
public static NumberFormatter oneDigit
public static NumberFormatter fourFractions
public static NumberFormatter twoFractions
public NumberFormatter(java.lang.String f)
public java.lang.String format(double d)
public static java.lang.String ISOtime()
public static java.lang.String ISOtime(java.util.Calendar c)
public static java.lang.String ISOdate()
public static java.lang.String ISOdate(java.util.Calendar c)
public static java.lang.String ISOweekTime()
public static java.lang.String ISOweekTime(java.util.Calendar c)
public static java.lang.String timeStamp(java.lang.String isoTime)
public static java.lang.String timeStamp(java.util.Calendar c)
public static java.lang.String timeStamp()
public static java.lang.String formatMS(long ms)
public static void main(java.lang.String[] argv)