How to get the first digit of a float number in Java

1 Answer

0 votes
public class Program {

    public static int getFirstDigit(float x) {
        String s = Float.toString(x);

        return Integer.parseInt(s.substring(0, 1));
    }

    public static void main(String args[]) {
        float f = 98.5271f;

        System.out.println(getFirstDigit(f));   
    }
}


/*
run:
     
9
        
*/

 



answered Aug 27, 2019 by avibootz
edited Sep 15 by avibootz
...