Hey, I don’t know if this counts as on topic, but is there any way to convert a double to a string and control the precision? Like, I need to be able to print a number to the console, but I need the number rounded so there are no more than 12 digits after the decimal point (this includes numbers listed in scientific notation). Is there any way to do that without manually changing each character in a string so that it’s rounded properly?
1 Like
You might use String.format(pattern, number) like this
double n;
String s;
// Test 1
n = 22d/7d;
s = String.format("%1.12e", n);
println("\n" + n + "\n" + s + " " + s.length());
// Test 2
n = -Math.exp(100d);
s = String.format("%.12e", n);
println("\n" + n + "\n" + s + " " + s.length());
1 Like