fishfry said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.
hello,
this should work ... but you can always search google on examples of the use
of "DecimalFormat"
greetz,
pet0etie
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
/\/\/\/\/\/\/\
public class someClass {
// number of digits to show (3 in tour example but easy to modify)
private int numDigits = 3;
public someClass (int value) {
System.out.print("Your original value " + value + " becomes : " +
formatValue(value));
}
private String formatValue() {
private DecimalFormat myFormat = createFormat(numDigits);
return myFormat.format(Value);
}
private DecimalFormat createFormat(int numDigits){
// Create a String that is all 0's, and the length is numDigits.
StringBuffer zeros = new StringBuffer();
for (int digit = 1; digit <= numDigits; digit++) {
zeros.append("0");
}
// Now use that to create a DecimalFormat object.
return new DecimalFormat(zeros);
}
}