text: // examples for printing floating point numbers
  
  import java.util.*;
  import java.io.*;
  import java.text.DecimalFormat;
  
  public class DecFormat {
      public static void main(String[] args) {
          DecimalFormat fmt;
  
          // round to at most 2 digits, leave of digits if not needed
          fmt = new DecimalFormat("#.##");
          System.out.println(fmt.format(12345.6789)); // produces 12345.68
          System.out.println(fmt.format(12345.0)); // produces 12345
          System.out.println(fmt.format(0.0)); // produces 0
          System.out.println(fmt.format(0.01)); // produces .1
  
          // round to precisely 2 digits
          fmt = new DecimalFormat("#.00");
          System.out.println(fmt.format(12345.6789)); // produces 12345.68
          System.out.println(fmt.format(12345.0)); // produces 12345.00
          System.out.println(fmt.format(0.0)); // produces .00
  
          // round to precisely 2 digits, force leading zero
          fmt = new DecimalFormat("0.00");
          System.out.println(fmt.format(12345.6789)); // produces 12345.68
          System.out.println(fmt.format(12345.0)); // produces 12345.00
          System.out.println(fmt.format(0.0)); // produces 0.00
  
          // round to precisely 2 digits, force leading zeros
          fmt = new DecimalFormat("000000000.00");
          System.out.println(fmt.format(12345.6789)); // produces 000012345.68
          System.out.println(fmt.format(12345.0)); // produces 000012345.00
          System.out.println(fmt.format(0.0)); // produces 000000000.00
  
          // force leading '+'
          fmt = new DecimalFormat("+0;-0");
          System.out.println(fmt.format(12345.6789)); // produces +12346
          System.out.println(fmt.format(-12345.6789)); // produces -12346
          System.out.println(fmt.format(0)); // produces +0
  
          // force leading positive/negative, pad to 2
          fmt = new DecimalFormat("positive 00;negative 0");
          System.out.println(fmt.format(1)); // produces "positive 01"
          System.out.println(fmt.format(-1)); // produces "negative 01"
  
          // qoute special chars (#)
          fmt = new DecimalFormat("text with '#' followed by #");
          System.out.println(fmt.format(12.34)); // produces "text with # followed by 12"
  
          // always show "."
          fmt = new DecimalFormat("#.#");
          fmt.setDecimalSeparatorAlwaysShown(true);
          System.out.println(fmt.format(12.34)); // produces "12.3"
          System.out.println(fmt.format(12)); // produces "12."
          System.out.println(fmt.format(0.34)); // produces "0.3"
  
          // different grouping distances:
          fmt = new DecimalFormat("#,####.###");
          System.out.println(fmt.format(123456789.123)); // produces "1,2345,6789.123"
  
          // scientific:
          fmt = new DecimalFormat("0.000E00");
          System.out.println(fmt.format(123456789.123)); // produces "1.235E08"
          System.out.println(fmt.format(-0.000234)); // produces "-2.34E-04"
  
          // using variable number of digits:
          fmt = new DecimalFormat("0");
          System.out.println(fmt.format(123.123)); // produces "123"
          fmt.setMinimumFractionDigits(8);
          System.out.println(fmt.format(123.123)); // produces "123.12300000"
          fmt.setMaximumFractionDigits(0);
          System.out.println(fmt.format(123.123)); // produces "123"
  
          // note: to pad with spaces, you need to do it yourself:
          // String out = fmt.format(...)
          // while (out.length() < targlength) out = " "+out;
      }
  }
  