Get number of digits before decimal point

前端 未结 25 2033
独厮守ぢ
独厮守ぢ 2020-12-28 11:53

I have a variable of decimal type and I want to check the number of digits before decimal point in it. What should I do? For example, 467.45 should

25条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 12:04

    This would be the Java solution

    public class test {
        public static void main(String args[]) {
            float f = 1.123f;
            int a = (int) f;
            int digits = 0;
            while (a > 0) {
                digits++;
                a=a/10;
            }
            System.out.println("No Of digits before decimal="+digits);
        }
    }
    

提交回复
热议问题