[Solved] Why is this code giving me a strange output


This is not because of the complier. It is happening because you are doing

i /= 10; //slice end

So when you do 13.4 after the first run it wont give you 1.34 it will give you something like 1.339999999999999999 which is 1.34.

Check Retain precision with double in Java for more details.

If you just want to reverse the number you can do

private static String numToString(double i) {
        String returnString = new StringBuilder(Double.toString(i)).reverse().toString();
        return i>=0?returnString:"-"+returnString.substring(0,returnString.length()-1);
}

4

solved Why is this code giving me a strange output