Package org.hipparchus.fraction
Class FractionFormat
- java.lang.Object
-
- java.text.Format
-
- java.text.NumberFormat
-
- org.hipparchus.fraction.FractionFormat
-
- All Implemented Interfaces:
Serializable
,Cloneable
- Direct Known Subclasses:
ProperFractionFormat
public class FractionFormat extends NumberFormat
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.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.text.NumberFormat
NumberFormat.Field
-
-
Field Summary
-
Fields inherited from class java.text.NumberFormat
FRACTION_FIELD, INTEGER_FIELD
-
-
Constructor Summary
Constructors Constructor Description FractionFormat()
Create an improper formatting instance with the default number format for the numerator and denominator.FractionFormat(NumberFormat format)
Create an improper formatting instance with a custom number format for both the numerator and denominator.FractionFormat(NumberFormat numeratorFormat, NumberFormat denominatorFormat)
Create an improper formatting instance with a custom number format for the numerator and a custom number format for the denominator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringBuffer
format(double value, StringBuffer buffer, FieldPosition position)
Formats a double value as a fraction and appends the result to a StringBuffer.StringBuffer
format(long value, StringBuffer buffer, FieldPosition position)
Formats a long value as a fraction and appends the result to a StringBuffer.StringBuffer
format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
Formats an object and appends the result to a StringBuffer.StringBuffer
format(Fraction fraction, StringBuffer toAppendTo, FieldPosition pos)
Formats aFraction
object to produce a string.static String
formatFraction(Fraction f)
This static method calls formatFraction() on a default instance of FractionFormat.static Locale[]
getAvailableLocales()
Get the set of locales for which complex formats are available.protected static NumberFormat
getDefaultNumberFormat()
Create a default number format.protected static NumberFormat
getDefaultNumberFormat(Locale locale)
Create a default number format.NumberFormat
getDenominatorFormat()
Access the denominator format.static FractionFormat
getImproperInstance()
Returns the default complex format for the current locale.static FractionFormat
getImproperInstance(Locale locale)
Returns the default complex format for the given locale.NumberFormat
getNumeratorFormat()
Access the numerator format.static FractionFormat
getProperInstance()
Returns the default complex format for the current locale.static FractionFormat
getProperInstance(Locale locale)
Returns the default complex format for the given locale.Fraction
parse(String source)
Parses a string to produce aFraction
object.Fraction
parse(String source, ParsePosition pos)
Parses a string to produce aFraction
object.protected static void
parseAndIgnoreWhitespace(String source, ParsePosition pos)
Parsessource
until a non-whitespace character is found.protected static char
parseNextCharacter(String source, ParsePosition pos)
Parsessource
until a non-whitespace character is found.-
Methods inherited from class java.text.NumberFormat
clone, equals, format, format, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, hashCode, isGroupingUsed, isParseIntegerOnly, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode
-
Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
-
-
-
-
Constructor Detail
-
FractionFormat
public FractionFormat()
Create an improper formatting instance with the default number format for the numerator and denominator.
-
FractionFormat
public FractionFormat(NumberFormat format)
Create an improper formatting instance with a custom number format for both the numerator and denominator.- Parameters:
format
- the custom format for both the numerator and denominator.- Throws:
NullArgumentException
- if the provided format is null.
-
FractionFormat
public FractionFormat(NumberFormat numeratorFormat, 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
- the custom format for the numerator.denominatorFormat
- the custom format for the denominator.- Throws:
NullArgumentException
- if either provided format is null.
-
-
Method Detail
-
getAvailableLocales
public static Locale[] getAvailableLocales()
Get the set of locales for which complex formats are available. This is the same set as theNumberFormat
set.- Returns:
- available complex format locales.
-
formatFraction
public static String formatFraction(Fraction f)
This static method calls formatFraction() on a default instance of FractionFormat.- Parameters:
f
- Fraction object to format- Returns:
- a formatted fraction in proper form.
-
getImproperInstance
public static FractionFormat getImproperInstance()
Returns the default complex format for the current locale.- Returns:
- the default complex format.
-
getImproperInstance
public static FractionFormat getImproperInstance(Locale locale)
Returns the default complex format for the given locale.- Parameters:
locale
- the specific locale used by the format.- Returns:
- the complex format specific to the given locale.
-
getProperInstance
public static FractionFormat getProperInstance()
Returns the default complex format for the current locale.- Returns:
- the default complex format.
-
getProperInstance
public static FractionFormat getProperInstance(Locale locale)
Returns the default complex format for the given locale.- Parameters:
locale
- the specific locale used by the format.- Returns:
- the complex format specific to the given locale.
-
getDefaultNumberFormat
protected static NumberFormat getDefaultNumberFormat()
Create a default number format. The default number format is based onNumberFormat.getNumberInstance(java.util.Locale)
with the only customizing is the maximum number of fraction digits, which is set to 0.- Returns:
- the default number format.
-
format
public StringBuffer format(Fraction fraction, StringBuffer toAppendTo, FieldPosition pos)
Formats aFraction
object to produce a string. The fraction is output in improper format.- Parameters:
fraction
- the object to format.toAppendTo
- where the text is to be appendedpos
- On input: an alignment field, if desired. On output: the offsets of the alignment field- Returns:
- the value passed in as toAppendTo.
-
format
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) throws MathIllegalArgumentException, MathIllegalStateException
Formats an object and appends the result to a StringBuffer.obj
must be either aFraction
object or aNumber
object. Any other type of object will result in anIllegalArgumentException
being thrown.- Overrides:
format
in classNumberFormat
- Parameters:
obj
- the object to format.toAppendTo
- where the text is to be appendedpos
- On input: an alignment field, if desired. On output: the offsets of the alignment field- Returns:
- the value passed in as toAppendTo.
- Throws:
MathIllegalStateException
- if the number cannot be converted to a fractionMathIllegalArgumentException
- ifobj
is not a valid type.- See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
-
parse
public Fraction parse(String source) throws MathIllegalStateException
Parses a string to produce aFraction
object.- Overrides:
parse
in classNumberFormat
- Parameters:
source
- the string to parse- Returns:
- the parsed
Fraction
object. - Throws:
MathIllegalStateException
- if the beginning of the specified string cannot be parsed.
-
parse
public Fraction parse(String source, ParsePosition pos)
Parses a string to produce aFraction
object. This method expects the string to be formatted as an improper fraction.- Specified by:
parse
in classNumberFormat
- Parameters:
source
- the string to parsepos
- input/output parsing parameter.- Returns:
- the parsed
Fraction
object.
-
getDefaultNumberFormat
protected static NumberFormat getDefaultNumberFormat(Locale locale)
Create a default number format. The default number format is based onNumberFormat.getNumberInstance(java.util.Locale)
. The only customization is the maximum number of BigFraction digits, which is set to 0.- Parameters:
locale
- the specific locale used by the format.- Returns:
- the default number format specific to the given locale.
-
getDenominatorFormat
public NumberFormat getDenominatorFormat()
Access the denominator format.- Returns:
- the denominator format.
-
getNumeratorFormat
public NumberFormat getNumeratorFormat()
Access the numerator format.- Returns:
- the numerator format.
-
parseAndIgnoreWhitespace
protected static void parseAndIgnoreWhitespace(String source, ParsePosition pos)
Parsessource
until a non-whitespace character is found.- Parameters:
source
- the string to parsepos
- input/output parsing parameter. On output,pos
holds the index of the next non-whitespace character.
-
parseNextCharacter
protected static char parseNextCharacter(String source, ParsePosition pos)
Parsessource
until a non-whitespace character is found.- Parameters:
source
- the string to parsepos
- input/output parsing parameter.- Returns:
- the first non-whitespace character.
-
format
public StringBuffer format(double value, StringBuffer buffer, FieldPosition position)
Formats a double value as a fraction and appends the result to a StringBuffer.- Specified by:
format
in classNumberFormat
- Parameters:
value
- the double value to formatbuffer
- StringBuffer to append toposition
- On input: an alignment field, if desired. On output: the offsets of the alignment field- Returns:
- a reference to the appended buffer
- See Also:
NumberFormat.format(Object, StringBuffer, FieldPosition)
-
format
public StringBuffer format(long value, StringBuffer buffer, FieldPosition position)
Formats a long value as a fraction and appends the result to a StringBuffer.- Specified by:
format
in classNumberFormat
- Parameters:
value
- the long value to formatbuffer
- StringBuffer to append toposition
- On input: an alignment field, if desired. On output: the offsets of the alignment field- Returns:
- a reference to the appended buffer
- See Also:
NumberFormat.format(Object, StringBuffer, FieldPosition)
-
-