I recently ran in the following curiosity:
double d=0.8608278036117554;
String precision="float";
float f=(float)d;
String s="a " + (precision.equals("float") ? (float)d: d) + " Bonjour";
String s2="a " + String.valueOf(precision.equals("float") ? (float)d: d) + " Bonjour";
String s3="a " + (precision.equals("float") ? String.valueOf((float)d): String.valueOf(d)) + " Bonjour";
println(d);
println(f);
println(s);
println(s2);
println(s3);
//0.8608278036117554
//0.8608278
//a 0.8608278036117554 Bonjour
//a 0.8608278036117554 Bonjour
//a 0.8608278 Bonjour
Why is it not possible to have a nice float when computing s or s2 ? Why is it still a double in case s and s2?
PS: This question has nothing to do with number formatting, in my opinion. I really want to understand why is the (float)d cast not working!!