Getting the Nth Decimal of a Float

前端 未结 3 1651
长情又很酷
长情又很酷 2021-01-17 08:16

I was trying to compare between decimals in floats using if statements, but I don\'t know how to do it. I searched a lot but didn\'t get the answer.

For example:

3条回答
  •  梦毁少年i
    2021-01-17 08:39

    You can take a Double and call toString on it and then iterate over charArray like this

    public class TestIndexOf {
        public static void main(String[] args) {
            Double d = 5.26;
            String source = d.toString();
    
            char[] chars = source.toCharArray();
    
            char max = source.charAt(source.length() - 1);
            boolean isMax = true;
            for (char aChar : chars) {
                if (max < aChar) {
                    max = aChar;
                    isMax = false;
                }
            }
            System.out.println(max + " is max?" + isMax);
        }
    }
    

提交回复
热议问题