Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4248151

RFE: DecimalFormat needs a way to express leading spaces

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.2
    • core-libs
    • sparc
    • solaris_7

      Customer has a problem with formatted output. He wants to write a VERY
      simple program. Read doubles from file, and print these doubles
      and their square roots to an output file, neatly formatted into
      two columns (with a blank between) where each double takes 8
      characters, i.e. 5 positions for integer part, 1 for decimal
      point, and 2 for fractional part:


      The input file
      - - - - - - -
         1.1
        22.2
       333.3
      4444.4


      Desired output file, "line format" is "iiiii.ff iiiii.ff"
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          1.10 1.05
         22.20 4.71
        333.30 18.26
       4444.40 66.67


      Below, is enclosed a (working) C program, and a vain attempt to
      write a Java program that does the same thing:
       

      The corresponding C program
      - - - - - - - - - - - - - -
      #include <stdio.h>
      #include <math.h>

      main() {
          FILE *fip; /* Input file */
          FILE *fop; /* Output file */
          char buf[256]; /* Input file line */
          double x;

          fip = fopen("testin.txt", "r");
          fop = fopen("testout.txt", "w");
          while (fgets(buf, 255, fip)) {
              sscanf(buf, "%lf", &x);
              fprintf(fop, "%8.2lf %8.2lf\n", x, sqrt(x));
          }
          fclose(fip);
          fclose(fop);
      }


      The C output file (which is exactly what I want)
      - - - - - - - - - - - - - - - - - - - - - - - -
          1.10 1.05
         22.20 4.71
        333.30 18.26
       4444.40 66.67


      The Java program
      - - - - - - - -
      import java.io.*;
      import java.text.*;

      class Test {
          public static void main(String args[]) throws IOException {
              java.util.Locale.setDefault(java.util.Locale.ENGLISH);

              FileInputStream is = new FileInputStream("testin.txt");
              FileOutputStream os = new FileOutputStream("testout.txt");
              BufferedReader br = new BufferedReader(new InputStreamReader(is));
              PrintWriter pw = new PrintWriter(os);

              DecimalFormat df = new DecimalFormat("####0.00"); // *** PATTERN ***

              for (String s = br.readLine(); s != null; s = br.readLine()) {
                  double x = Double.parseDouble(s);
                  pw.println(df.format(x)+" "+df.format(Math.sqrt(x)));
              }

              is.close();
              pw.flush();
              os.close();
          }
      }


      Try 1. Java output file, if I use DecimalFormat pattern "####0.00":
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      1.10 1.05
      22.20 4.71
      333.30 18.26
      4444.40 66.67


      Try 2. Java output file, if I use DecimalFormat pattern "00000.00":
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      00001.10 00001.05
      00022.20 00004.71
      00333.30 00018.26
      04444.40 00066.67


      Try 3. Java output file, if I use DecimalFormat pattern " 0.00":
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          1.10 1.05
          22.20 4.71
          333.30 18.26
          4444.40 66.67


      Desired output file; is this really IMPOSSIBLE to get?
      - - - - - - - - - - - - - - - - - - - - - - - - - - -
          1.10 1.05
         22.20 4.71
        333.30 18.26
       4444.40 66.67


      There should be a simple DecimalFormat formatting character that
      says "pad to a fixed width replacing leading zeros with spaces".
      -- linden@eng, June 21 1999

            nlindenbsunw Norbert Lindenberg (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: