Four implementations of java that preserve decimals

  • 2020-05-17 05:27:42
  • OfStack

The example in this paper is to answer the problem of keeping two decimal places in java for your reference. The specific content is as follows

Method 1:

4 5 in


double  f  =  111231.5585; 
BigDecimal  b  =  new  BigDecimal(f); 
double  f1  =  b.setScale(2,  BigDecimal.ROUND_HALF_UP).doubleValue(); 

Mode 1 is a good class to solve the problem caused by mode 3.

Let's keep two decimal places

Method 2:


DecimalFormat  df  =new DecimalFormat("#.00"); 
df.format( The number you want to format );

Example: new DecimalFormat (" #. 00). format (3.1415926)

.00 means two decimal places, #.00004 and so on...

Note:

When I was using the above class, there was a data conversion exception. The reason was that a comma appeared when String was converting double, resulting in a conversion error. In many European countries, the decimal point is represented by a comma. In many European countries, such as France and the Netherlands, the decimal point is not written in this way, but 3,14. In these countries, the decimal point is represented by a comma. Instead of a comma, 333,333,333 is separated by a dot, which is 333.333.333. There are so many European and American countries, and the number expression method of each country is not the same one system.

Method 3:


double d = 3.1415926;
String result = String .format("%.2f");

%.2 f %. Means: any number of 2 before the decimal point means two decimal places and the result is f for floating point

Method 4:


NumberFormat ddf1=NumberFormat.getNumberInstance() ;

void setMaximumFractionDigits(int digits) 

The number of digits shown by digits
Sets the maximum number of bits to be displayed after the decimal point for the formatting object, and the last bit to be 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 common format for 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 for the current default locale. 
 myString = NumberFormat.getPercentInstance().format(test); 
 System.out.println(myString);

 //setMaximumFractionDigits(int)  Sets the maximum number of digits allowed in the fractional part of the value  
 //setMaximumIntegerDigits(int)  Sets the maximum number of digits allowed for the integer portion of the value  
 //setMinimumFractionDigits(int)  Sets the minimum number of digits allowed in the fractional part of a value  
 //setMinimumIntegerDigits(int)  Sets the minimum number of bits allowed for the integer part of the value 

Related articles: