Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public class DecimalFormat
- Object
- Format
- NumberFormat
- DecimalFormat
ImplementsCloneable, Serializable
Formats and parses decimal numbers according to a pattern.
Pattern syntax
A pattern is a positive subpattern, optionally followed by ; and a
negative subpattern of which only the prefix and suffix are used. Each
subpattern is a prefix, a number and a suffix. In the number:
0is a digit that is always written,#a digit written only when it is significant..is the decimal separator and,the grouping separator; the distance from the last,to the end of the integer part is the group size.Efollowed by one or more0selects scientific notation, with at least that many exponent digits. With more integer digit positions than0s among them (##0.###E0), the exponent is kept a multiple of the number of integer positions.
In a prefix or suffix, % multiplies the number by 100 and writes a
percent sign, the per mille sign multiplies by 1000, the currency sign
writes the currency symbol (doubled, the international currency code), and
- writes the minus sign. A single quote makes the characters up to the
next one literal, and two in a row stand for one quote.
The characters actually written – separators, minus, percent, currency
– come from the format’s DecimalFormatSymbols.
Rounding
Rounding is half-even, the JDK default, and like the JDK it is decided by
the exact binary value of a double: 0.125 is a true tie and formats as
0.12 under 0.00, while 0.15, which as a double lies just below the
decimal 0.15, formats as 0.1 under 0.0.
The JDK departs from half-even in two places, and so does this class,
because a last digit that differs from the desktop’s is worse than either
answer: a whole number rounds a tie up in scientific notation (12345
under 0.###E0 is 1.235E4), and 5E-4, 5E-5 and so on round down
when the 5 is the first digit dropped (0.0005 under 0.000 is 0.000).
Differences from the JDK
- No
setRoundingMode,setCurrency,setParseBigDecimalorformatToCharacterIterator: the device has none of the types they name. - No localized patterns (
applyLocalizedPattern,toLocalizedPattern).
Constructors
public DecimalFormat() | |
public DecimalFormat(String pattern) | |
public DecimalFormat(String pattern, DecimalFormatSymbols symbols) |
Methods
Inherited fields
Inherited methods
From NumberFormat
getInstance, getInstance, getNumberInstance, getNumberInstance, getIntegerInstance, getIntegerInstance, getPercentInstance, getPercentInstance, getCurrencyInstance, getCurrencyInstance, format, format, format, parse, parseObject, isParseIntegerOnly, setParseIntegerOnly, isGroupingUsed, setGroupingUsed, getMaximumIntegerDigits, setMaximumIntegerDigits, getMinimumIntegerDigits, setMinimumIntegerDigits, getMaximumFractionDigits, setMaximumFractionDigits, getMinimumFractionDigits, setMinimumFractionDigits
From Format
Constructor details
DecimalFormat
public DecimalFormat()DecimalFormat
public DecimalFormat(String pattern)DecimalFormat
public DecimalFormat(String pattern, DecimalFormatSymbols symbols)Method details
format
public StringBuffer format(double number, StringBuffer result, FieldPosition fieldPosition)format
public StringBuffer format(long number, StringBuffer result, FieldPosition fieldPosition)parse
public Number parse(String text, ParsePosition pos)parsePosition, which is advanced past
what was read. On failure the position is left alone, its error index
is set, and null is returned. The result is a Long when the text is
a whole number that fits one, and a Double otherwise.applyPattern
public void applyPattern(String pattern)toPattern
public String toPattern()0.00 comes back as
#0.00.getDecimalFormatSymbols
public DecimalFormatSymbols getDecimalFormatSymbols()setDecimalFormatSymbols(DecimalFormatSymbols).setDecimalFormatSymbols
public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)getPositivePrefix
public String getPositivePrefix()setPositivePrefix
public void setPositivePrefix(String newValue)getNegativePrefix
public String getNegativePrefix()setNegativePrefix
public void setNegativePrefix(String newValue)getPositiveSuffix
public String getPositiveSuffix()setPositiveSuffix
public void setPositiveSuffix(String newValue)getNegativeSuffix
public String getNegativeSuffix()setNegativeSuffix
public void setNegativeSuffix(String newValue)getMultiplier
public int getMultiplier()setMultiplier
public void setMultiplier(int newValue)getGroupingSize
public int getGroupingSize()setGroupingSize
public void setGroupingSize(int newValue)isDecimalSeparatorAlwaysShown
public boolean isDecimalSeparatorAlwaysShown()setDecimalSeparatorAlwaysShown
public void setDecimalSeparatorAlwaysShown(boolean newValue)clone
public Object clone()Object.clone()
answers null on the device for anything but an array.equals
public boolean equals(Object obj)hashCode
public int hashCode()