OK, I see this is fairly old, but I just came across it, and maybe someone will still care.
My first two reactions to the code were (1) break some of those nested blocks out into separate methods, for readability if nothing else, and (2) I've never seen so many unnecessary String constructors in one method. In fact I'm pretty sure every single use of "new String" is unnecessary, since the argument to the constructor is already a String. I'm not sure why the Java language even includes a String(String) constructor, as its only possible use seems to be to construct teaching examples on garbage collection and the difference between == and .equals().
But I digress...
On further reading though, I decided there would be little point to refining the code in its current form. Read on for reasons...
> First, Java simplifies all doubles to the format it wishes.
> For instance, I want to keep this value as a double: 1.123e2.
> Nope, says Java, I'm gonna have to change you to this: 112.3!
> Different amount of charaters completely!
Since the two numbers you give are in fact equal, they are stored in the double datatype exactly the same way. What you want is to control the format of the string representation of the number. This is probably best handled with the DecimalFormat class, hidden away in the java.text package like Junilu suggested. Here's a quick demo, which also shows how you can
test your method to verify its behavior:
<code><pre>
import java.text.*;
class Formatter {
static String format(double value, int integerDigits, int fractionalDigits) {
DecimalFormat df = new DecimalFormat();
df.setMaximumIntegerDigits(integerDigits);
df.setMinimumIntegerDigits(integerDigits);
df.setMaximumFractionDigits(fractionalDigits);
df.setMinimumFractionDigits(fractionalDigits);
return df.format(value);
}
}
class Test {
public static void main(String[] s) throws Throwable {
test(1.23, 0, 0, "");
test(1.23, 1, 0, "1");
test(1.23, 1, 1, "1.2");
test(1.27, 1, 1, "1.3");
test(1.23, 1, 2, "1.23");
test(1.23, 2, 5, "01.23000");
test(-51.3, 2, 5, "-51.30000");
test(123, 2, 5, "??.???");
System.out.println("Test completed");
}
public static void test(double value, int integerDigits, int fractionalDigits,
String expectedResult) {
String actualResult = Formatter.format(value, integerDigits, fractionalDigits);
if (!expectedResult.equals(actualResult)) {
System.out.println("Failed for");
System.out.println(" value = " + value);
System.out.println(" integerDigits = " + integerDigits);
System.out.println(" fractionalDigits = " + fractionalDigits);
System.out.println(" actualResult = " + actualResult);
}
}
}</pre></code>
I included two tests which the method currently fails, since this was intended as a simple demo, and I'm not even really sure what expected behavior you want to have. Many more possibilities are present - read the docs on DecimalFormat carefully. It takes some study, but is pretty powerful.
> Second, Java's math library is stupid in this case. The only
> rounding methods it has are toInt() and toDouble().
You probably shouldn't call a library stupid if you're unfamiliar with it. For real rounding, in the sense of methods that actually modify the value of a number rather than merely control the String representation of the number, look at Math.ceil(), Math.floor(), and Math.round(), as well as the methods in BigDecimal.