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

 

     /**
     * 取得單一數值小數點後有幾位
     */
    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;
    }

 

測試

輸入:截圖 2020-01-14 下午9.43.14

結果:截圖 2020-01-14 下午9.42.47

 

輸入:截圖 2020-01-14 下午9.44.01

輸出:截圖 2020-01-14 下午9.44.28

 

輸入:截圖 2020-01-14 下午9.45.13

輸出:截圖 2020-01-14 下午9.45.43

 

輸入:截圖 2020-01-14 下午9.46.24

輸出:截圖 2020-01-14 下午9.48.25

arrow
arrow

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