Top Description Fields Constructors Methods
org.apache.commons.math3.fraction

public Class FractionFormat

extends AbstractFormat
Class Inheritance
Known Direct Subclasses
org.apache.commons.math3.fraction.ProperFractionFormat
Imports
java.text.FieldPosition, .NumberFormat, .ParsePosition, java.util.Locale, org.apache.commons.math3.exception.MathIllegalArgumentException, .MathParseException, org.apache.commons.math3.exception.util.LocalizedFormats

Formats a Fraction number in proper format or improper format. The number format for each of the whole number, numerator and, denominator can be configured.
Since
1.1

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public
FractionFormat()

Create an improper formatting instance with the default number format for the numerator and denominator.

public
FractionFormat(final NumberFormat
the custom format for both the numerator and denominator.
format
)

Create an improper formatting instance with a custom number format for both the numerator and denominator.

public
FractionFormat(final NumberFormat
the custom format for the numerator.
numeratorFormat
,
final NumberFormat
the custom format for the denominator.
denominatorFormat
)

Create an improper formatting instance with a custom number format for the numerator and a custom number format for the denominator.

Method Summary

Modifier and TypeMethod and Description
public StringBuffer

Returns:

the value passed in as toAppendTo.
format
(final Fraction
the object to format.
fraction
,
final StringBuffer
where the text is to be appended
toAppendTo
,
final FieldPosition
On input: an alignment field, if desired. On output: the offsets of the alignment field
pos
)

Formats a Fraction object to produce a string.

public StringBuffer

Returns:

the value passed in as toAppendTo.
format
(final Object
the object to format.
obj
,
final StringBuffer
where the text is to be appended
toAppendTo
,
final FieldPosition
On input: an alignment field, if desired. On output: the offsets of the alignment field
pos
)

Overrides java.text.NumberFormat.format.

Formats an object and appends the result to a StringBuffer.

public static String

Returns:

a formatted fraction in proper form.
formatFraction
(Fraction
Fraction object to format
f
)

This static method calls formatFraction() on a default instance of FractionFormat.

public static Locale[]

Returns:

available complex format locales.
getAvailableLocales
()

Hides java.text.NumberFormat.getAvailableLocales.

Get the set of locales for which complex formats are available.

protected static NumberFormat

Returns:

the default number format.
getDefaultNumberFormat
()

Hides org.apache.commons.math3.fraction.AbstractFormat.getDefaultNumberFormat.

Create a default number format.

public static FractionFormat

Returns:

the default complex format.
getImproperInstance
()

Returns the default complex format for the current locale.

public static FractionFormat

Returns:

the complex format specific to the given locale.
getImproperInstance
(final Locale
the specific locale used by the format.
locale
)

Returns the default complex format for the given locale.

public static FractionFormat

Returns:

the default complex format.
getProperInstance
()

Returns the default complex format for the current locale.

public static FractionFormat

Returns:

the complex format specific to the given locale.
getProperInstance
(final Locale
the specific locale used by the format.
locale
)

Returns the default complex format for the given locale.

public Fraction

Returns:

the parsed Fraction object.
parse
(final String
the string to parse
source
)

Overrides java.text.NumberFormat.parse.

Parses a string to produce a Fraction object.

public Fraction

Returns:

the parsed Fraction object.
parse
(final String
the string to parse
source
,
final ParsePosition
input/output parsing parameter.
pos
)

Implements abstract java.text.NumberFormat.parse.

Parses a string to produce a Fraction object.

Inherited from org.apache.commons.math3.fraction.AbstractFormat:
formatformatgetDefaultNumberFormatgetDenominatorFormatgetNumeratorFormatparseAndIgnoreWhitespaceparseNextCharactersetDenominatorFormatsetNumeratorFormat

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.commons.math3.fraction.AbstractFormat.serialVersionUID.

Serializable version identifier

Constructor Detail

FractionFormatback to summary
public FractionFormat()

Create an improper formatting instance with the default number format for the numerator and denominator.

FractionFormatback to summary
public FractionFormat(final NumberFormat format)

Create an improper formatting instance with a custom number format for both the numerator and denominator.

Parameters
format:NumberFormat

the custom format for both the numerator and denominator.

FractionFormatback to summary
public FractionFormat(final NumberFormat numeratorFormat, final NumberFormat denominatorFormat)

Create an improper formatting instance with a custom number format for the numerator and a custom number format for the denominator.

Parameters
numeratorFormat:NumberFormat

the custom format for the numerator.

denominatorFormat:NumberFormat

the custom format for the denominator.

Method Detail

formatback to summary
public StringBuffer format(final Fraction fraction, final StringBuffer toAppendTo, final FieldPosition pos)

Formats a Fraction object to produce a string. The fraction is output in improper format.

Parameters
fraction:Fraction

the object to format.

toAppendTo:StringBuffer

where the text is to be appended

pos:FieldPosition

On input: an alignment field, if desired. On output: the offsets of the alignment field

Returns:StringBuffer

the value passed in as toAppendTo.

formatback to summary
public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) throws FractionConversionException, MathIllegalArgumentException

Overrides java.text.NumberFormat.format.

Formats an object and appends the result to a StringBuffer. obj must be either a Fraction object or a Number object. Any other type of object will result in an IllegalArgumentException being thrown.

Parameters
obj:Object

the object to format.

toAppendTo:StringBuffer

where the text is to be appended

pos:FieldPosition

On input: an alignment field, if desired. On output: the offsets of the alignment field

Returns:StringBuffer

the value passed in as toAppendTo.

Annotations
@Override
Exceptions
FractionConversionException:
if the number cannot be converted to a fraction
MathIllegalArgumentException:
if obj is not a valid type.
See Also
java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
formatFractionback to summary
public static String formatFraction(Fraction f)

This static method calls formatFraction() on a default instance of FractionFormat.

Parameters
f:Fraction

Fraction object to format

Returns:String

a formatted fraction in proper form.

getAvailableLocalesback to summary
public static Locale[] getAvailableLocales()

Hides java.text.NumberFormat.getAvailableLocales.

Get the set of locales for which complex formats are available. This is the same set as the NumberFormat set.

Returns:Locale[]

available complex format locales.

getDefaultNumberFormatback to summary
protected static NumberFormat getDefaultNumberFormat()

Hides org.apache.commons.math3.fraction.AbstractFormat.getDefaultNumberFormat.

Create a default number format. The default number format is based on NumberFormat#getNumberInstance(java.util.Locale) with the only customizing is the maximum number of fraction digits, which is set to 0.

Returns:NumberFormat

the default number format.

getImproperInstanceback to summary
public static FractionFormat getImproperInstance()

Returns the default complex format for the current locale.

Returns:FractionFormat

the default complex format.

getImproperInstanceback to summary
public static FractionFormat getImproperInstance(final Locale locale)

Returns the default complex format for the given locale.

Parameters
locale:Locale

the specific locale used by the format.

Returns:FractionFormat

the complex format specific to the given locale.

getProperInstanceback to summary
public static FractionFormat getProperInstance()

Returns the default complex format for the current locale.

Returns:FractionFormat

the default complex format.

getProperInstanceback to summary
public static FractionFormat getProperInstance(final Locale locale)

Returns the default complex format for the given locale.

Parameters
locale:Locale

the specific locale used by the format.

Returns:FractionFormat

the complex format specific to the given locale.

parseback to summary
public Fraction parse(final String source) throws MathParseException

Overrides java.text.NumberFormat.parse.

Parses a string to produce a Fraction object.

Parameters
source:String

the string to parse

Returns:Fraction

the parsed Fraction object.

Annotations
@Override
Exceptions
MathParseException:
if the beginning of the specified string cannot be parsed.
parseback to summary
public Fraction parse(final String source, final ParsePosition pos)

Implements abstract java.text.NumberFormat.parse.

Parses a string to produce a Fraction object. This method expects the string to be formatted as an improper fraction.

Parameters
source:String

the string to parse

pos:ParsePosition

input/output parsing parameter.

Returns:Fraction

the parsed Fraction object.

Annotations
@Override