public final class TypeFormat
extends java.lang.Object
This class provides utility methods 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 (see benchmark).
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). For example:
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 java.lang.Appendable |
format(boolean b,
java.lang.Appendable a)
Formats the specified
boolean and appends the resulting
text to the Appendable argument. |
static java.lang.Appendable |
format(double d,
java.lang.Appendable a)
Formats the specified
double value (16 or 17 digits output). |
static java.lang.Appendable |
format(double d,
int digits,
boolean scientific,
boolean showZero,
java.lang.Appendable a)
Formats the specified
double value according to the
specified formatting arguments. |
static java.lang.Appendable |
format(float f,
java.lang.Appendable a)
Formats the specified
float value. |
static java.lang.Appendable |
format(int i,
java.lang.Appendable a)
Formats the specified
int and appends the resulting
text (decimal representation) to the Appendable argument. |
static java.lang.Appendable |
format(int i,
int radix,
java.lang.Appendable a)
Formats the specified
int in the specified radix and appends
the resulting text to the Appendable argument. |
static java.lang.Appendable |
format(long l,
java.lang.Appendable a)
Formats the specified
long and appends the resulting
text (decimal representation) to the Appendable argument. |
static java.lang.Appendable |
format(long l,
int radix,
java.lang.Appendable a)
Formats the specified
long in the specified radix and
appends the resulting text to the Appendable argument. |
static boolean |
parseBoolean(java.lang.CharSequence csq)
Parses the specified character sequence as a
boolean . |
static boolean |
parseBoolean(java.lang.CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a
boolean . |
static boolean |
parseBoolean(java.lang.String str)
Equivalent to
parseBoolean(CharSequence)
(for J2ME compatibility). |
static byte |
parseByte(java.lang.CharSequence csq)
Parses the specified character sequence as a signed decimal
byte . |
static byte |
parseByte(java.lang.CharSequence csq,
int radix)
Parses the specified character sequence as a signed
byte
in the specified radix. |
static byte |
parseByte(java.lang.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(java.lang.CharSequence csq)
Parses the specified character sequence as a
double . |
static double |
parseDouble(java.lang.CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a
double . |
static double |
parseDouble(java.lang.String str)
Equivalent to
parseDouble(CharSequence)
(for J2ME compatibility). |
static float |
parseFloat(java.lang.CharSequence csq)
Parses the specified character sequence as a
float . |
static float |
parseFloat(java.lang.CharSequence csq,
Cursor cursor)
Parses the specified character sequence from the specified position
as a
float . |
static float |
parseFloat(java.lang.String str)
Equivalent to
parseFloat(CharSequence)
(for J2ME compatibility). |
static int |
parseInt(java.lang.CharSequence csq)
Parses the specified character sequence as a signed
int . |
static int |
parseInt(java.lang.CharSequence csq,
int radix)
Parses the specified character sequence as a signed
int
in the specified radix. |
static int |
parseInt(java.lang.CharSequence csq,
int radix,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed
int in the specified radix. |
static int |
parseInt(java.lang.String str)
Equivalent to
parseInt(CharSequence) (for J2ME compatibility). |
static int |
parseInt(java.lang.String str,
int radix)
Equivalent to
parseInt(CharSequence, int)
(for J2ME compatibility). |
static long |
parseLong(java.lang.CharSequence csq)
Parses the specified character sequence as a decimal
long . |
static long |
parseLong(java.lang.CharSequence csq,
int radix)
Parses the specified character sequence as a signed
long
in the specified radix. |
static long |
parseLong(java.lang.CharSequence csq,
int radix,
Cursor cursor)
Parses the specified character sequence from the specified position
as a signed
long in the specified radix. |
static long |
parseLong(java.lang.String str)
Equivalent to
parseLong(CharSequence)
(for J2ME compatibility). |
static long |
parseLong(java.lang.String str,
int radix)
Equivalent to
parseLong(CharSequence, int)
(for J2ME compatibility). |
static short |
parseShort(java.lang.CharSequence csq)
Parses the specified character sequence as a signed decimal
short . |
static short |
parseShort(java.lang.CharSequence csq,
int radix)
Parses the specified character sequence as a signed
short
in the specified radix. |
static short |
parseShort(java.lang.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(java.lang.CharSequence csq)
boolean
.csq
- the character sequence to parse.parseBoolean(csq, null)
java.lang.IllegalArgumentException
- if the specified character sequence
is different from "true" or "false" ignoring cases.public static boolean parseBoolean(java.lang.String str)
parseBoolean(CharSequence)
(for J2ME compatibility).public static boolean parseBoolean(java.lang.CharSequence csq, Cursor cursor)
boolean
.csq
- the character sequence to parse.cursor
- the cursor position (being maintained) or
null>
to parse the whole character sequence.java.lang.IllegalArgumentException
- if the character sequence from the
specified position is different from "true" or "false" ignoring
cases.public static byte parseByte(java.lang.CharSequence csq)
byte
.csq
- the character sequence to parse.parseByte(csq, 10)
java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable byte
.parseByte(CharSequence, int)
public static byte parseByte(java.lang.CharSequence csq, int radix)
byte
in the specified radix.csq
- the character sequence to parse.radix
- the radix to be used while parsing.byte
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable byte
.public static byte parseByte(java.lang.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 maintained) or
null>
to parse the whole character sequence.byte
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable byte
.public static short parseShort(java.lang.CharSequence csq)
short
.csq
- the character sequence to parse.parseShort(csq, 10)
java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable short
.parseShort(CharSequence, int)
public static short parseShort(java.lang.CharSequence csq, int radix)
short
in the specified radix.csq
- the character sequence to parse.radix
- the radix to be used while parsing.short
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable short
.public static short parseShort(java.lang.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 maintained) or
null>
to parse the whole character sequence.short
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable short
.public static int parseInt(java.lang.CharSequence csq)
int
.csq
- the character sequence to parse.parseInt(csq, 10)
java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable int
.parseInt(CharSequence, int)
public static int parseInt(java.lang.String str)
parseInt(CharSequence)
(for J2ME compatibility).public static int parseInt(java.lang.CharSequence csq, int radix)
int
in the specified radix.csq
- the character sequence to parse.radix
- the radix to be used while parsing.int
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable int
.public static int parseInt(java.lang.String str, int radix)
parseInt(CharSequence, int)
(for J2ME compatibility).public static int parseInt(java.lang.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 maintained) or
null>
to parse the whole character sequence.int
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable int
.public static long parseLong(java.lang.CharSequence csq)
long
.csq
- the character sequence to parse.parseLong(csq, 10)
java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable long
.parseLong(CharSequence, int)
public static long parseLong(java.lang.String str)
parseLong(CharSequence)
(for J2ME compatibility).public static long parseLong(java.lang.CharSequence csq, int radix)
long
in the specified radix.csq
- the character sequence to parse.radix
- the radix to be used while parsing.long
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable long
.public static long parseLong(java.lang.String str, int radix)
parseLong(CharSequence, int)
(for J2ME compatibility).public static long parseLong(java.lang.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 maintained) or
null>
to parse the whole character sequence.long
.java.lang.NumberFormatException
- if the specified character sequence
does not contain a parsable long
.public static float parseFloat(java.lang.CharSequence csq)
float
.csq
- the character sequence to parse.public static float parseFloat(java.lang.String str)
parseFloat(CharSequence)
(for J2ME compatibility).public static float parseFloat(java.lang.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 double parseDouble(java.lang.CharSequence csq) throws java.lang.NumberFormatException
double
.
The format must be of the form:
<decimal>{'.'<fraction>}{'E|e'<exponent>}
.csq
- the character sequence to parse.java.lang.NumberFormatException
- if the character sequence does not contain
a parsable double
.public static double parseDouble(java.lang.String str)
parseDouble(CharSequence)
(for J2ME compatibility).public static double parseDouble(java.lang.CharSequence csq, Cursor cursor) throws java.lang.NumberFormatException
double
.csq
- the character sequence to parse.cursor
- the cursor position (being maintained) or
null>
to parse the whole character sequence.java.lang.NumberFormatException
- if the character sequence does not contain
a parsable double
.public static java.lang.Appendable format(boolean b, java.lang.Appendable a) throws java.io.IOException
boolean
and appends the resulting
text to the Appendable
argument.b
- a boolean
.a
- the Appendable
to append.StringBuffer
object.java.io.IOException
- if an I/O exception occurs.public static java.lang.Appendable format(int i, java.lang.Appendable a) throws java.io.IOException
int
and appends the resulting
text (decimal representation) to the Appendable
argument.i
- the int
number.a
- the Appendable
to append.Appendable
object.java.io.IOException
- if an I/O exception occurs.public static java.lang.Appendable format(int i, int radix, java.lang.Appendable a) throws java.io.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.java.lang.IllegalArgumentException
- if radix is not in [2 .. 36] range.java.io.IOException
- if an I/O exception occurs.public static java.lang.Appendable format(long l, java.lang.Appendable a) throws java.io.IOException
long
and appends the resulting
text (decimal representation) to the Appendable
argument.l
- the long
number.a
- the Appendable
to append.Appendable
object.java.io.IOException
- if an I/O exception occurs.parseLong(java.lang.CharSequence)
public static java.lang.Appendable format(long l, int radix, java.lang.Appendable a) throws java.io.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.java.lang.IllegalArgumentException
- if radix is not in [2 .. 36] range.java.io.IOException
- if an I/O exception occurs.parseLong(CharSequence, int)
public static java.lang.Appendable format(float f, java.lang.Appendable a) throws java.io.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)
java.io.IOException
- if an I/O exception occurs.public static java.lang.Appendable format(double d, java.lang.Appendable a) throws java.io.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)
java.io.IOException
- if an I/O exception occurs.TextBuilder.append(double)
public static java.lang.Appendable format(double d, int digits, boolean scientific, boolean showZero, java.lang.Appendable a) throws java.io.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.java.lang.IllegalArgumentException
- if (digits > 19)
)java.io.IOException
- if an I/O exception occurs.TextBuilder.append(double, int, boolean, boolean)
Copyright © 2005 - 2007 Javolution.