public class NumberFormatter extends 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 String |
append
What shall be appended
|
static NumberFormatter |
fiveDigits
Predefined NumberFormatter for #####
|
protected 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(String f)
Creates a NumberFormatter for a pattern
|
| Modifier and Type | Method and Description |
|---|---|
String |
format(double d)
Converts a double to a String
|
static String |
formatMS(long ms)
Converts milliseconds to a nice String
|
static String |
ISOdate()
Returns an ISO8601 string representation of the current date
|
static String |
ISOdate(Calendar c)
Returns an ISO8601 string representation of the time given
by the calendar
|
static String |
ISOtime()
Returns an ISO8601 string representation of the current time
|
static String |
ISOtime(Calendar c)
Returns an ISO8601 string representation of the time given
by the calendar
|
static String |
ISOweekTime()
Returns an ISO8601 string representation of the current time,
using a week-oriented representation
|
static String |
ISOweekTime(Calendar c)
Returns an ISO8601 string representation of the time given
by the calendar, using a week-oriented representation
|
static void |
main(String[] argv)
Test routine
|
static String |
timeStamp()
Returns a current timestamp
|
static String |
timeStamp(Calendar c)
Returns a timestamp from a Calendar
|
static String |
timeStamp(String isoTime)
Returns a timestamp from an ISOweekTime or an ISOtime
|
protected int integers
protected int fractions
protected boolean showPosSign
protected char separator
protected Pattern FORMAT
protected 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(String f)
public String format(double d)
public static String ISOtime()
public static String ISOtime(Calendar c)
public static String ISOdate()
public static String ISOdate(Calendar c)
public static String ISOweekTime()
public static String ISOweekTime(Calendar c)
public static String timeStamp(String isoTime)
public static String timeStamp()
public static String formatMS(long ms)
public static void main(String[] argv)
Copyright © 2018. All rights reserved.