Top Description Fields Constructors Methods
org.apache.poi.ss.usermodel

public Class FractionFormat

extends Format
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.math.BigDecimal, java.text.FieldPosition, .Format, .ParsePosition, java.util.regex.Matcher, .Pattern, org.apache.logging.log4j.LogManager, .Logger, org.apache.poi.ss.format.SimpleFraction, org.apache.poi.ss.formula.eval.NotImplementedException

Format class that handles Excel style fractions, such as "# #/#" and "#/###"

As of this writing, this is still not 100% accurate, but it does a reasonable job of trying to mimic Excel's fraction calculations. It does not currently maintain Excel's spacing.

This class relies on a method lifted nearly verbatim from org.apache.math.fraction. If further uses for Commons Math are found, we will consider adding it as a dependency. For now, we have in-lined the one method to keep things simple.

Field Summary

Modifier and TypeField and Description
private static final Pattern
private final int
private static final Logger
private static final int
private final int
private final String

Constructor Summary

AccessConstructor and Description
public
FractionFormat(String wholePartFormatString, String
The format string for the denominator
denomFormatString
)

Single parameter ctor

Method Summary

Modifier and TypeMethod and Description
public String
private String
format(final BigDecimal decimal)

public StringBuffer
format(Object
The object to format
obj
,
StringBuffer
where the text is to be appended
toAppendTo
,
FieldPosition
A FieldPosition identifying a field in the formatted text
pos
)

Implements abstract java.text.Format.format.

Formats an object and appends the resulting text to a given string buffer.

public Object
parseObject(String
the String to parse
source
,
ParsePosition
A ParsePosition object with index and error index information as described above.
pos
)

Implements abstract java.text.Format.parseObject.

Parses text from the given string to produce an object.

Inherited from java.text.Format:
cloneformatformatToCharacterIteratorparseObject

Field Detail

DENOM_FORMAT_PATTERNback to summary
private static final Pattern DENOM_FORMAT_PATTERN
exactDenomback to summary
private final int exactDenom
LOGGERback to summary
private static final Logger LOGGER
MAX_DENOM_POWback to summary
private static final int MAX_DENOM_POW
maxDenomback to summary
private final int maxDenom
wholePartFormatStringback to summary
private final String wholePartFormatString

Constructor Detail

FractionFormatback to summary
public FractionFormat(String wholePartFormatString, String denomFormatString)

Single parameter ctor

Parameters
denomFormatString:String

The format string for the denominator

Method Detail

formatback to summary
public String format(Number num)
Annotations
@SuppressWarnings:squid:S2111
formatback to summary
private String format(final BigDecimal decimal)
Annotations
@SuppressWarnings:squid:S2111
formatback to summary
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)

Implements abstract java.text.Format.format.

Doc from java.text.Format.format.

Formats an object and appends the resulting text to a given string buffer. If the pos argument identifies a field used by the format, then its indices are set to the beginning and end of the first such field encountered.

Parameters
obj:Object

The object to format

toAppendTo:StringBuffer

where the text is to be appended

pos:FieldPosition

A FieldPosition identifying a field in the formatted text

Returns:StringBuffer

the string buffer passed in as toAppendTo, with formatted text appended

Annotations
@Override
parseObjectback to summary
public Object parseObject(String source, ParsePosition pos)

Implements abstract java.text.Format.parseObject.

Doc from java.text.Format.parseObject.

Parses text from the given string to produce an object.

This method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed object is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

Parameters
source:String

the String to parse

pos:ParsePosition

A ParsePosition object with index and error index information as described above.

Returns:Object

An Object parsed from the string. In case of error, returns null.

Annotations
@Override