The example of this article for Java reserved two decimal questions for everyone to answer for your reference, the specific contents are as follows
Mode one:
Rounded
Double f = 111231.5585;
BigDecimal B = new BigDecimal (f);
Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();
Mode one this class solves the problem of mode three very well.
Keep two decimal digits
Mode two:
DecimalFormat DF =new DecimalFormat ("#.00");
Df.format (the number you want to format);
Example:new DecimalFormat ("#.00"). Format (3.1415926)
.00 indicates a two-bit decimal #.0000 four-bit decimal, and so on ...
Attention:
I have a data transformation exception when I use this class, because a comma occurs when a string converts a double, resulting in a conversion error. A number of European countries, the decimal point is expressed by commas, 3.14 in many European countries, such as France, the Netherlands, such as not to write, but 3, 14. In these countries, the decimal point we are talking about is represented by commas. and 333,333,333 of the spacer character is not a comma, but a point to express, that is, 333.333.333. So many countries in Europe and America, each country's digital expression is not the same system.
Mode three:
Double d = 3.1415926;
string result = string. Format ("%.2f");
%.2f%. Indicates that the result of an arbitrary number of digits before the decimal point of 2 indicates a two-bit decimal format, and an F indicates a floating-point type
Mode four:
NumberFormat ddf1=numberformat.getnumberinstance ();
void setmaximumfractiondigits (int digits)
Number of digits displayed by digits
Sets the maximum number of digits to display after the decimal point is set for the formatted object, and the last bit displayed is rounded
Specific analysis:
Returns the default numeric format for the current default locale.
String myString = numberformat.getinstance (). Format (mynumber);
System.out.println (myString);
Getcurrencyinstance () returns the general format of the current default locale
myString = numberformat.getcurrencyinstance (). Format (mynumber);
System.out.println (myString);
Getnumberinstance () returns the common numeric format for the current default locale.
myString = numberformat.getnumberinstance (). Format (mynumber);
System.out.println (myString);
Getpercentinstance () returns the percentage format of the current default locale.
myString = numberformat.getpercentinstance (). Format (test);
System.out.println (myString);
setmaximumfractiondigits (int) Sets the maximum number of digits allowed for the decimal part of a numeric value
//setmaximumintegerdigits (int) sets the maximum digits allowed for the integer portion of a numeric value
// setminimumfractiondigits (int) Sets the minimum number of digits allowed for the decimal part of a numeric value
//setminimumintegerdigits (int) Sets the minimum number of digits allowed for the integer portion of a numeric value
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.