2010-01-19 90 views

回答

5

所以,我設法端口桌面Java實現:bbnumberformat

String[] patterns = new String[] { "#,#00.00#", "0.0;(0.0)", 
      "0.###E0" }; 
    DecimalFormat format = (DecimalFormat) DecimalFormat 
      .getNumberInstance(); 
    double value = -12.321; 
    for (int i = 0; i < patterns.length; i++) { 
     String pattern = patterns[i]; 
     format.applyPattern(pattern); 
     String text = "Pattern: " + pattern 
     + " Sample: "+format.format(value)+"\n"; 
     add(new LabelField(text)); 
    } 

alt text http://img220.imageshack.us/img220/6245/9000.jpg

2

下面的代碼是方法,演示如何圓一個雙到N小數:

private double round(double num,int numDecim){ 
    long p=1; 
    //next line – calculate pow(10,brDecim) 
    for(int i=0; i<numDecim; i++)p*=10; 
    return (double)(int)(p * num + 0.5)/p; 
} 

您可以改爲使用雙重p=1.0;p*=10.0;