I was writing a program to convert numbers after the decimal point to binary numbers, and I wanted to handle the numbers added to the list other than the extended for statement.
Eclipse Neon 4.6.3 Java8
String variable name= String.join(Characters you want to concatenate,Array name);
public class forPractice {
    public static void main(String[] args) {
        double x = 0.125;
        List<String> y = new ArrayList<String>();
        while (x > 0) {
            x *= 2;
            if (x >= 1) {
                y.add("1");
                x -= 1;
            } else {
                y.add("0");
            }
        }
        String z = String.join("", y);
        System.out.print("0." + z);
    }
}
Execution result
0.001
If it was completed on one screen, only the extended for statement was enough, but I felt that the for statement was difficult to handle when creating JSP using Servlet, so I decided to use this as well.
Recommended Posts