Java decimal point several post-processing

@ Way: 

// rounding   
Double    F =    111,231.5585 ;   
the BigDecimal B    =    new new    the BigDecimal (F);  
 Double    F1 = b.setScale ( 2 , BigDecimal.ROUND_HALF_UP) .doubleValue ();   
// two decimal places  
 ---- -------------------------------------------------- ---------   

Second way: 

the java.text.DecimalFormat DF    = new new    the java.text.DecimalFormat ( " # .00 " );   
df.format (you to be formatted); 

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

# 00 represent two decimal places # .0000 four decimal places and so on ... 

Three ways: 

Double d = 3.1415926 ; 

String the Result = String .format ( " % .2f " , d);

 % .2f% decimal point. before any digits    2 shows the results after two decimal floating-point format f represents 

four ways: 

the NumberFormat ddf1 = the NumberFormat.getNumberInstance (); 

void setMaximumFractionDigits ( int digits) 
digits displayed digits // 
// is formatted target setting up the display position after the decimal point, is displayed last bit rounded 

Import the java.text. * ; 
Import Classes in java.math. * ; 
 class TT 
{ 
  public  static void main(String args[]) 
  { 
    
double x=23.5455;     NumberFormat ddf1=NumberFormat.getNumberInstance() ;     ddf1.setMaximumFractionDigits(2);     String s= ddf1.format(x) ;     System.out.print(s);   } }

 

Guess you like

Origin www.cnblogs.com/muxi0407/p/12049039.html