DecimalFormat contains a pattern and a set of symbol meanings:
01 Digits
# A number that does not include 0
. Placeholder for decimal separator
, the placeholder for the grouping separator
; Delimited format.
-Default negative number prefix.
% multiplied by 100 and displayed as a percentage
? Multiplied by 1000 and displayed as a binary currency symbol, replaced with a currency sign;
International currency symbol instead. If it appears in a pattern, use the currency decimal separator to substitute
Replace the decimal separator character.
Any other character used in the X prefix or suffix to refer to special characters in the prefix or suffix.
Example:
Results:
12.3
12.3
012.340
12.34
Append: (Common in Java reserved 2-bit decimal method)
Way One:
Rounded
Double F = 111231.5585;
BigDecimal B = new BigDecimal (f);
Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();
Keep Two decimal places
---------------------------------------------------------------
Way two:
Java.text.DecimalFormat DF =new Java.text.DecimalFormat ("#.00");
Df.format (the number you want to format);
Example: New Java.text.DecimalFormat ("#.00"). Format (3.1415926)
#.00 represents two decimal places #.0000 four decimal places and so on ...
Way three:
Double d = 3.1415926;
string result = string. Format ("%.2f");
%.2f%. Indicates that any number of digits before the decimal point 2 indicates that the result of the two-bit decimal format is F for floating-point
Mode four:
NumberFormat ddf1=numberformat.getnumberinstance ();
void setmaximumfractiondigits (int digits)
Digits number of digits displayed
Sets the maximum number of digits to display for the formatted object after the decimal point, the last bit shown is rounded
Classes BigDecimal and DecimalFormat used in the calculation of business data in Java