Java將小數轉換為成百分比輸出


 1 import java.text.DecimalFormat;
 2 import java.text.NumberFormat;
 3 
 4 import org.junit.Test;
 5 
 6 public class TestCase {
 7     /**
 8      * 將小數裝換成百分比輸出
 9      * 將double類型保留小數點后兩位,轉換成
10      */
11     @Test
12     public void test(){
13 //      ================================================================================        
14         double f = 0.5585;  
15 //        BigDecimal  b = new BigDecimal(f);  
16 //        double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
17 //        System.out.println(f1);
18         System.out.println(Integer.parseInt(new DecimalFormat("0").format(f*100))+"%");//百分比沒有小數點
19 //      ===========================首選===================================================
20         double result1=0.51111122111111;
21         DecimalFormat df = new DecimalFormat("0.00%");
22         String r = df.format(result1);
23         System.out.println(r);//great
24 //      =================================================================================        
25         NumberFormat num = NumberFormat.getPercentInstance(); 
26         num.setMaximumIntegerDigits(3); 
27         num.setMaximumFractionDigits(2); 
28         double csdn = 0.55555555555555555; 
29         System.out.println(num.format(csdn));//good
30 //      =================================================================================        
31         double result=1;
32         int temp = (int)(result * 1000);
33         result = (double)temp / 10;
34         System.out.println(result + "%");//100%  變成了  100.0%
35     }
36 
37 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM