3

For a project I need to print some numbers to the printer. They need to be printed below eachother, aligned on the comma. The font is not and can not be a monospaced one. I'm using a DecimalFormat("0.000") to transform all the numbers into strings ending in exactly 3 decimals.

What would be the correct way to align the numbers on the comma, below eachother? I'm using Java's Printable interface, as explained here http://docs.oracle.com/javase/tutorial/2d/printing/index.html.

Thanks

EDIT: I'm not trying to print to the console, but to a printer (the actual hardware thing where real paper comes out)

RDM
  • 4,986
  • 4
  • 34
  • 43

2 Answers2

1

This would certainly look better with a monospaced font.

I'm assuming that the comma is the decimal point. Here's what I would do.

  • Generate a comma image using drawString.

  • Break up the number into two parts, the number before the decimal, and the number after the decimal.

  • Generate the number images before the decimal and the number after the decimal using drawString.

  • After all the numbers are converted to images, go through the numbers after the decimal images to see which one has the longest width.

  • You now have the number of pixels of width for the longest numbers after the decimal, and the comma.

  • Go through the number images again, drawing your images on the printer canvas in the following order.

    • Number after the decimal
    • Comma
    • Number before the decimal
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Thanks for the advice. I tried to do something similar, splitting the number on the decimal point and printing the decimal part on a fixed point prefixed by a comma, and the prefixing the integer part. The problem is I can't figure out the anchor x for the integer part as it has variable length. I've tried `fontMetrics.charsWidth(...)` and throwing in the integer part of the number (as a string) as a parameter, but that doesn't work properly. I think I'm just going to emulate a monospaced font by defining a charwidth and iterating over the string.toCharArray(). Thanks again! – RDM Feb 12 '13 at 15:49
  • On a sidenote, I'm having trouble making the printer print monospaced (on a different project...). Sure enough I'm setting the graphics environment's font to a monospaced font (`new Font("Monospaced", Font.PLAIN, 12)`) which is producing monospaced prints on Mac but not on Windows. Is this a printer manufacturer driver issue or can I do anything about it? I've also tried the Courier and Consolas fonts. – RDM Feb 12 '13 at 16:05
  • 1
    http://stackoverflow.com/questions/11020978/monospaced-font-symbols-for-jtextpane – Gilbert Le Blanc Feb 12 '13 at 16:23
  • Thanks, this is more or less what I had though. I've tried setting the Font name to "Courier New" but I'm still struggling with it as now I don't get anything monospaced at all anymore, neither on Mac nor on Windows. – RDM Feb 12 '13 at 16:39
0

Just convert them to aligned strings and then print these strings:

public static String align (String str)
{
    int n = str.indexOf('.');

    StringBuilder sb = new StringBuilder ();
    for (int i = n; i < 3; i++)
        sb.append ((char)0x2007); // Append FIGURE-SPACE character
    sb.append (str);
    return sb.toString ();
}

public static void main(String[] args) {
    Random r = new Random ();
    DecimalFormat format = new DecimalFormat("0.000");

    for (int i = 0; i < 10; i++)
    {
        String str = format.format(Math.pow (r.nextDouble(), 3.0) * 150.0); 
        System.out.println(align (str));
    }
}
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • Unfortunately I'm not printing to the console, I'm printing to a printer device using graphics.drawString(String str, int x, int y), and the string str is printed starting at location x, y from left to right (so left-aligned). The console is monospaced and that would be easy... – RDM Feb 12 '13 at 14:14
  • Even in non-monospace fonts, all digits usually have the same width, and there is special unicode character "FIGURE-SPACE" (U+2007) that has the same width as digits. – Mikhail Vladimirov Feb 12 '13 at 14:32