判斷小數點位數的方法如下

 

     /**
     * 取得單一數值小數點後有幾位
     */
    private static int getDec(double input) {
        String stringIn = String.valueOf(input);

        int i = stringIn.substring(stringIn.indexOf(".")).length() - 1;
        if (i == 1 && stringIn.substring(stringIn.indexOf(".") + 1).matches("0")) {
            i--;
        }
        return i;
    }

 

測試

輸入:

結果:

 

輸入:

輸出:

 

輸入:

輸出:

 

輸入:

輸出:

文章標籤
全站熱搜
創作者介紹
創作者 碼農日常 的頭像
碼農日常

碼農日常大小事

碼農日常 發表在 痞客邦 留言(2) 人氣(976)