Package org.python.core.stringlib
Class FloatFormatter
java.lang.Object
org.python.core.stringlib.InternalFormat.Formatter
org.python.core.stringlib.FloatFormatter
- All Implemented Interfaces:
Appendable
A class that provides the implementation of floating-point formatting. In a limited way, it acts
like a StringBuilder to which text and one or more numbers may be appended, formatted according
to the format specifier supplied at construction. These are ephemeral objects that are not, on
their own, thread safe.
-
Constructor Summary
ConstructorsConstructorDescriptionFloatFormatter(StringBuilder result, InternalFormat.Spec spec) Construct the formatter from a client-supplied buffer, to which the result will be appended, and a specification.Construct the formatter from a specification, allocating a buffer internally for the result. -
Method Summary
Modifier and TypeMethodDescriptionappend(char c) append(CharSequence csq) append(CharSequence csq, int start, int end) format(double value) Format a floating-point number according to the specification represented by thisFloatFormatter.Format a floating-point number according to the specification represented by thisFloatFormatter.voidsetMinFracDigits(int minFracDigits) Override the default truncation behaviour for the specification originally supplied.static intsize(InternalFormat.Spec spec) Recommend a buffer size for a given specification, assuming one float is converted.Methods inherited from class org.python.core.stringlib.InternalFormat.Formatter
alignmentNotAllowed, alternateFormNotAllowed, alternateFormNotAllowed, getPyResult, getResult, isBytes, notAllowed, notAllowed, pad, precisionNotAllowed, precisionTooLarge, setBytes, setStart, signNotAllowed, toString, unknownFormat, zeroPaddingNotAllowed
-
Constructor Details
-
FloatFormatter
Construct the formatter from a client-supplied buffer, to which the result will be appended, and a specification. SetsInternalFormat.Formatter.markto the end of the buffer.- Parameters:
result- destination bufferspec- parsed conversion specification
-
FloatFormatter
Construct the formatter from a specification, allocating a buffer internally for the result.- Parameters:
spec- parsed conversion specification
-
-
Method Details
-
size
Recommend a buffer size for a given specification, assuming one float is converted. This will be a "right" answer for e and g-format, and for f-format with values up to 9,999,999.- Parameters:
spec- parsed conversion specification
-
setMinFracDigits
public void setMinFracDigits(int minFracDigits) Override the default truncation behaviour for the specification originally supplied. Some formats remove trailing zero digits, trimming to zero or one. Set memberminFracDigits, to modify this behaviour.- Parameters:
minFracDigits- if <0 prevent truncation; if ≥0 the minimum number of fractional digits; when this is zero, and all fractional digits are zero, the decimal point will also be removed.
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classInternalFormat.Formatter
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classInternalFormat.Formatter
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classInternalFormat.Formatter- Throws:
IndexOutOfBoundsException
-
format
Format a floating-point number according to the specification represented by thisFloatFormatter.- Parameters:
value- to convert- Returns:
- this object
-
format
Format a floating-point number according to the specification represented by thisFloatFormatter. The conversion type, precision, and flags for grouping or percentage are dealt with here. At the point this is used, we know theInternalFormat.Formatter.specis one of the floating-point types. This entry point allows explicit control of the prefix of positive numbers, overriding defaults for the format type.- Parameters:
value- to convertpositivePrefix- to use before positive values (e.g. "+") or null to default to ""- Returns:
- this object
-