for (int count = 0; count < numColumns; count++) {
float rel = relParts[count] / relTotal * 100;
Float f = new Float(rel);
- widths[count] = Integer.toString(f.intValue()) + "%";
+ widths[count] = Integer.toString(f.intValue());
}
+ widths = correctRoundingError(widths);
} else {
int pixelWidth = nominalWidth;
for (int count = 0; count < numColumns; count++) {
float rel = relParts[count] / absTotal * 100;
Float f = new Float(rel);
- widths[count] = Integer.toString(f.intValue()) + "%";
+ widths[count] = Integer.toString(f.intValue());
}
+ widths = correctRoundingError(widths);
}
}
return rtf;
}
}
+
+ /**
+ * Correct rounding errors introduced in calculating the width of each
+ * column. Make sure they sum to 100% in the end.
+ */
+ protected static String[] correctRoundingError(String widths[]) {
+ int totalWidth = 0;
+
+ for (int count = 0; count < widths.length; count++) {
+ try {
+ int width = Integer.parseInt(widths[count]);
+ totalWidth += width;
+ } catch (NumberFormatException nfe) {
+ // nop; "can't happen"
+ }
+ }
+
+ float totalError = 100 - totalWidth;
+ float columnError = totalError / widths.length;
+ float error = 0;
+
+ for (int count = 0; count < widths.length; count++) {
+ try {
+ int width = Integer.parseInt(widths[count]);
+ error = error + columnError;
+ if (error >= 1.0) {
+ int adj = (int) Math.round(Math.floor(error));
+ error = error - (float) Math.floor(error);
+ width = width + adj;
+ widths[count] = Integer.toString(width) + "%";
+ } else {
+ widths[count] = Integer.toString(width) + "%";
+ }
+ } catch (NumberFormatException nfe) {
+ // nop; "can't happen"
+ }
+ }
+
+ return widths;
+ }
}
for (int count = 0; count < numColumns; count++) {
float rel = relParts[count] / relTotal * 100;
Float f = new Float(rel);
- widths[count] = Integer.toString(f.intValue()) + "%";
+ widths[count] = Integer.toString(f.intValue());
}
+ widths = correctRoundingError(widths);
} else {
int pixelWidth = nominalWidth;
for (int count = 0; count < numColumns; count++) {
float rel = relParts[count] / absTotal * 100;
Float f = new Float(rel);
- widths[count] = Integer.toString(f.intValue()) + "%";
+ widths[count] = Integer.toString(f.intValue());
}
+ widths = correctRoundingError(widths);
}
}
}
return attrs;
}
+
+ /**
+ * Correct rounding errors introduced in calculating the width of each
+ * column. Make sure they sum to 100% in the end.
+ */
+ protected String[] correctRoundingError(String widths[]) {
+ int totalWidth = 0;
+
+ for (int count = 0; count < widths.length; count++) {
+ try {
+ int width = Integer.parseInt(widths[count]);
+ totalWidth += width;
+ } catch (NumberFormatException nfe) {
+ // nop; "can't happen"
+ }
+ }
+
+ float totalError = 100 - totalWidth;
+ float columnError = totalError / widths.length;
+ float error = 0;
+
+ for (int count = 0; count < widths.length; count++) {
+ try {
+ int width = Integer.parseInt(widths[count]);
+ error = error + columnError;
+ if (error >= 1.0) {
+ int adj = (int) Math.round(Math.floor(error));
+ error = error - (float) Math.floor(error);
+ width = width + adj;
+ widths[count] = Integer.toString(width) + "%";
+ } else {
+ widths[count] = Integer.toString(width) + "%";
+ }
+ } catch (NumberFormatException nfe) {
+ // nop; "can't happen"
+ }
+ }
+
+ return widths;
+ }
}