@Realtime public final class TypeFormat extends Object
Utility class to parse CharSequence into primitive types and
to format primitive types into any Appendable.
Methods from this class do not create temporary objects and are typically faster than standard library methods.
The number of digits when formatting floating point numbers can be
specified. The default setting for double is 17 digits
or even 16 digits when the conversion is lossless back and forth
(mimic the standard library formatting).
TypeFormat.format(0.2, a) = "0.2" // 17 or 16 digits (as long as lossless conversion), remove trailing zeros. TypeFormat.format(0.2, 17, false, false, a) = "0.20000000000000001" // Closest 17 digits number. TypeFormat.format(0.2, 19, false, false, a) = "0.2000000000000000111" // Closest 19 digits. TypeFormat.format(0.2, 4, false, false, a) = "0.2" // Fixed-point notation, remove trailing zeros. TypeFormat.format(0.2, 4, false, true, a) = "0.2000" // Fixed-point notation, fixed number of digits. TypeFormat.format(0.2, 4, true, false, a) = "2.0E-1" // Scientific notation, remove trailing zeros. TypeFormat.format(0.2, 4, true, true, a) = "2.000E-1" // Scientific notation, fixed number of digits.
For non-primitive objects, formatting is typically performed using
specialized TextFormat instances.
| Modifier and Type | Method and Description |
|---|---|
static Appendable |
format(boolean b,
Appendable a)
Formats the specified
boolean and appends the resulting
text to the Appendable argument. |
static Appendable |
format(double d,
Appendable a)
Formats the specified
double value (16 or 17 digits output). |
static Appendable |
format(double d,
int digits,
boolean scientific,
boolean showZero,
Appendable a)
Formats the specified
double value according to the
specified formatting arguments. |
static Appendable |
format(float f,
Appendable a)
Formats the specified
float value. |
static Appendable |
format(int i,
Appendable a)
Formats the specified
int and appends the resulting
text (decimal representation) to the Appendable argument. |
static Appendable |
format(int i,
int radix,
Appendable a)
Formats the specified
int in the specified radix and appends
the resulting text to the Appendable argument. |
static Appendable |
format(long l,
Appendable a)
Formats the specified
long and appends the resulting
text (decimal representation) to the Appendable argument. |
static Appendable |
format(long l,
int radix,
Appendable a)
Formats the specified
long in the specified radix and
appends the resulting text to the Appendable argument. |
static boolean |
parseBoolean(CharSequence csq)
Parses the whole specified character sequence as a
boolean. |
static boolean |
parseBoolean(CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a
boolean ignoring cases. |
static byte |
parseByte(CharSequence csq)
Parses the whole specified character sequence as a signed decimal
byte. |
static byte |
parseByte(CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed decimal
byte. |
static byte |
parseByte(CharSequence csq,
int radix)
Parses the whole specified character sequence
as a signed
byte in the specified radix. |
static byte |
parseByte(CharSequence csq,
int radix,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed
byte in the specified radix. |
static double |
parseDouble(CharSequence csq)
Parses the whole specified character sequence as a
double. |
static double |
parseDouble(CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a
double. |
static float |
parseFloat(CharSequence csq)
Parses the whole specified character sequence as a
float. |
static float |
parseFloat(CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a
float. |
static int |
parseInt(CharSequence csq)
Parses the whole specified character sequence as a signed decimal
int. |
static int |
parseInt(CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed decimal
int. |
static int |
parseInt(CharSequence csq,
int radix)
Parses the whole specified character sequence
as a signed
int in the specified radix. |
static int |
parseInt(CharSequence csq,
int radix,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed
int in the specified radix. |
static long |
parseLong(CharSequence csq)
Parses the whole specified character sequence as a signed decimal
long. |
static long |
parseLong(CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed decimal
long. |
static long |
parseLong(CharSequence csq,
int radix)
Parses the whole specified character sequence
as a signed
long in the specified radix. |
static long |
parseLong(CharSequence csq,
int radix,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed
long in the specified radix. |
static short |
parseShort(CharSequence csq)
Parses the whole specified character sequence as a signed decimal
short. |
static short |
parseShort(CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed decimal
short. |
static short |
parseShort(CharSequence csq,
int radix)
Parses the whole specified character sequence
as a signed
short in the specified radix. |
static short |
parseShort(CharSequence csq,
int radix,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed
short in the specified radix. |
public static boolean parseBoolean(CharSequence csq, Cursor cursor)
boolean ignoring cases.csq - the character sequence to parse.cursor - the cursor position (being maintained).IllegalArgumentException - if the character sequence from the
specified position is different from "true" or "false" ignoring
cases.public static boolean parseBoolean(CharSequence csq)
boolean.csq - the character sequence to parse.parseBoolean(csq, new Cursor())IllegalArgumentException - if the specified character sequence
is different from "true" or "false" ignoring cases.public static byte parseByte(CharSequence csq, int radix, Cursor cursor)
byte in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.cursor - the cursor position being updated.byte.NumberFormatException - if the specified character sequence
does not contain a parsable byte.public static byte parseByte(CharSequence csq, int radix)
byte in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.byte.NumberFormatException - if the specified character sequence
does not contain a parsable byte or contains
extraneous characters.public static byte parseByte(CharSequence csq, Cursor cursor)
byte.csq - the character sequence to parse.cursor - the cursor position being updated.byte.NumberFormatException - if the specified character sequence
does not contain a parsable byte.public static byte parseByte(CharSequence csq)
byte.csq - the character sequence to parse.parseByte(csq, 10)NumberFormatException - if the specified character sequence
does not contain a parsable byte or contains
extraneous characters.public static short parseShort(CharSequence csq, int radix, Cursor cursor)
short in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.cursor - the cursor position being updated.short.NumberFormatException - if the specified character sequence
does not contain a parsable short.public static short parseShort(CharSequence csq, int radix)
short in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.short.NumberFormatException - if the specified character sequence
does not contain a parsable short or contains
extraneous characters.public static short parseShort(CharSequence csq, Cursor cursor)
short.csq - the character sequence to parse.cursor - the cursor position being updated.short.NumberFormatException - if the specified character sequence
does not contain a parsable short.public static short parseShort(CharSequence csq)
short.csq - the character sequence to parse.parseShort(csq, 10)NumberFormatException - if the specified character sequence
does not contain a parsable short or contains
extraneous characters.public static int parseInt(CharSequence csq, int radix, Cursor cursor)
int in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.cursor - the cursor position being updated.int.NumberFormatException - if the specified character sequence
does not contain a parsable int.public static int parseInt(CharSequence csq, int radix)
int in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.int.NumberFormatException - if the specified character sequence
does not contain a parsable int or contains
extraneous characters.public static int parseInt(CharSequence csq, Cursor cursor)
int.csq - the character sequence to parse.cursor - the cursor position being updated.int.NumberFormatException - if the specified character sequence
does not contain a parsable int.public static int parseInt(CharSequence csq)
int.csq - the character sequence to parse.parseInt(csq, 10)NumberFormatException - if the specified character sequence
does not contain a parsable int or contains
extraneous characters.public static long parseLong(CharSequence csq, int radix, Cursor cursor)
long in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.cursor - the cursor position being updated.long.NumberFormatException - if the specified character sequence
does not contain a parsable long.public static long parseLong(CharSequence csq, int radix)
long in the specified radix.csq - the character sequence to parse.radix - the radix to be used while parsing.long.NumberFormatException - if the specified character sequence
does not contain a parsable long or contains
extraneous characters.public static long parseLong(CharSequence csq, Cursor cursor)
long.csq - the character sequence to parse.cursor - the cursor position being updated.long.NumberFormatException - if the specified character sequence
does not contain a parsable long.public static long parseLong(CharSequence csq)
long.csq - the character sequence to parse.parseLong(csq, 10)NumberFormatException - if the specified character sequence
does not contain a parsable long or contains
extraneous characters.public static float parseFloat(CharSequence csq, Cursor cursor)
float.csq - the character sequence to parse.cursor - the cursor position (being maintained) or
null> to parse the whole character sequence.public static float parseFloat(CharSequence csq)
float.csq - the character sequence to parse.NumberFormatException - if the specified character sequence
does not contain a parsable long or contains
extraneous characters.public static double parseDouble(CharSequence csq, Cursor cursor) throws NumberFormatException
double.csq - the character sequence to parse.cursor - the cursor position (being maintained) or
null> to parse the whole character sequence.NumberFormatException - if the character sequence does not contain
a parsable double.public static double parseDouble(CharSequence csq) throws NumberFormatException
double.
The format must be of the form:
<decimal>{'.'<fraction>}{'E|e'<exponent>}.csq - the character sequence to parse.NumberFormatException - if the specified character sequence
does not contain a parsable long or contains
extraneous characters.public static Appendable format(boolean b, Appendable a) throws IOException
boolean and appends the resulting
text to the Appendable argument.b - a boolean.a - the Appendable to append.StringBuffer object.IOException - if an I/O exception occurs.public static Appendable format(int i, Appendable a) throws IOException
int and appends the resulting
text (decimal representation) to the Appendable argument.i - the int number.a - the Appendable to append.Appendable object.IOException - if an I/O exception occurs.public static Appendable format(int i, int radix, Appendable a) throws IOException
int in the specified radix and appends
the resulting text to the Appendable argument.i - the int number.radix - the radix.a - the Appendable to append.Appendable object.IllegalArgumentException - if radix is not in [2 .. 36] range.IOException - if an I/O exception occurs.public static Appendable format(long l, Appendable a) throws IOException
long and appends the resulting
text (decimal representation) to the Appendable argument.l - the long number.a - the Appendable to append.Appendable object.IOException - if an I/O exception occurs.parseLong(java.lang.CharSequence, int, javolution.text.Cursor)public static Appendable format(long l, int radix, Appendable a) throws IOException
long in the specified radix and
appends the resulting text to the Appendable argument.l - the long number.radix - the radix.a - the Appendable to append.Appendable object.IllegalArgumentException - if radix is not in [2 .. 36] range.IOException - if an I/O exception occurs.parseLong(CharSequence, int)public static Appendable format(float f, Appendable a) throws IOException
float value.f - the float value.a - the Appendable to append.TypeFormat.format(f, 10, (MathLib.abs(f) >= 1E7) || (MathLib.abs(f) < 0.001), false, a)IOException - if an I/O exception occurs.public static Appendable format(double d, Appendable a) throws IOException
double value (16 or 17 digits output).d - the double value.a - the Appendable to append.TypeFormat.format(d, -1, (MathLib.abs(d) >= 1E7) || (MathLib.abs(d) < 0.001), false, a)IOException - if an I/O exception occurs.TextBuilder.append(double)public static Appendable format(double d, int digits, boolean scientific, boolean showZero, Appendable a) throws IOException
double value according to the
specified formatting arguments.d - the double value.digits - the number of significative digits (excludes exponent) or
-1 to mimic the standard library (16 or 17 digits).scientific - true to forces the use of the scientific
notation (e.g. 1.23E3); false
otherwise.showZero - true if trailing fractional zeros are
represented; false otherwise.a - the Appendable to append.Appendable object.IllegalArgumentException - if (digits > 19))IOException - if an I/O exception occurs.TextBuilder.append(double, int, boolean, boolean)Copyright © 2005-2013 Javolution. All Rights Reserved.