java 對象轉整數,兩個整數相除轉百分數


 

 

public class MathUtil {
    
    public static void main(String[] args) {
        System.out.println(toPercent(1,3));
        System.out.println(toPercent(3,1));
    }
    
    //a/b轉百分數字符串 (double)Math.round( a/(double)b*1000)/1000:保留3位小數四舍五入
    private static String toPercent(int a,int b) {
        if (a%b==0) {
            return a/b*100+"%";
        }else {
            return (double)Math.round( a/(double)b*1000)/10+"%";
        }
    }
    
    //Object轉整數(失敗返回null)
    private Integer toInt(Object o) {
        if (o!=null && !"".equals(o.toString())) {
            try {
                int parseInt = Integer.parseInt(o.toString());
                return parseInt;
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
}

 


免責聲明!

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



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