Example usage for java.text DecimalFormat DecimalFormat

List of usage examples for java.text DecimalFormat DecimalFormat

Introduction

In this page you can find the example usage for java.text DecimalFormat DecimalFormat.

Prototype

public DecimalFormat(String pattern) 

Source Link

Document

Creates a DecimalFormat using the given pattern and the symbols for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFormattedTextField tft2 = new JFormattedTextField(new DecimalFormat("#.0"));
    tft2.setValue(new Float(123.4F));

    // Retrieve the value from the text field
    Float floatValue = (Float) tft2.getValue();

}

From source file:Main.java

public static void main(String[] args) {
    int areaCode = 123;
    int exchangeCode = 456;

    NumberFormat nf3 = new DecimalFormat("0000");

    System.out.println(nf3.format(areaCode) + "-" + nf3.format(exchangeCode));
}

From source file:HTMLDemo.java

public static void main(String[] a) throws IOException {
    PrintWriter pw = new PrintWriter(new FileWriter("test.html"));
    DecimalFormat ff = new DecimalFormat("#0"), cf = new DecimalFormat("0.0");
    pw.println("<TABLE BORDER><TR><TH>Fahrenheit<TH>Celsius</TR>");
    for (double f = 100; f <= 400; f += 10) {
        double c = 5 * (f - 32) / 9;
        pw.println("<TR ALIGN=RIGHT><TD>" + ff.format(f) + "<TD>" + cf.format(c));
    }//from w ww .  j  a v a2 s. co  m
    pw.println("</TABLE>");
    pw.close(); // Without this, the output file may be empty
}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat format = new DecimalFormat("####.##");
    format.setMinimumFractionDigits(2);//from w  ww . ja  v  a2s. com
    final JFormattedTextField field1 = new JFormattedTextField(format);
    final JFormattedTextField field2 = new JFormattedTextField(format);
    field1.setColumns(15);
    field2.setColumns(15);
    JButton btn = new JButton(new AbstractAction("Multiply by 2") {

        @Override
        public void actionPerformed(ActionEvent e) {
            Number value = (Number) field1.getValue();
            if (value != null) {
                field2.setValue(2 * value.doubleValue());
            }
        }
    });

    JPanel panel = new JPanel();
    panel.add(field1);
    panel.add(btn);
    panel.add(field2);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static void main(String[] argv) {
    JFormattedTextField f = new JFormattedTextField(new BigDecimal("123.4567"));
    DefaultFormatter fmt = new NumberFormatter(new DecimalFormat("#.0###############"));
    fmt.setValueClass(f.getValue().getClass());
    DefaultFormatterFactory fmtFactory = new DefaultFormatterFactory(fmt, fmt, fmt);
    f.setFormatterFactory(fmtFactory);/* w w  w .  j a v  a 2s.  c  o m*/

    BigDecimal bigValue = (BigDecimal) f.getValue();

}

From source file:Main.java

public static void main(String[] args) {
    long time = System.currentTimeMillis();
    Date d = new Date(time);
    Timestamp t = new Timestamp(time);
    t.setNanos(123456789);/*w  w  w  .  ja va  2 s .com*/
    System.out.println(d);
    System.out.println(t);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.'");
    NumberFormat nf = new DecimalFormat("000000000");
    System.out.println(df.format(t.getTime()) + nf.format(t.getNanos()));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Box form = Box.createVerticalBox();
    form.add(new JLabel("Name:"));
    form.add(new JTextField("User Name"));

    form.add(new JLabel("Birthday:"));
    JFormattedTextField birthdayField = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"));
    birthdayField.setValue(new Date());
    form.add(birthdayField);//from  ww  w.ja v a  2s  .co m

    form.add(new JLabel("Age:"));
    form.add(new JFormattedTextField(new Integer(32)));

    form.add(new JLabel("Hairs on Body:"));
    JFormattedTextField hairsField = new JFormattedTextField(new DecimalFormat("###,###"));
    hairsField.setValue(new Integer(100000));
    form.add(hairsField);

    form.add(new JLabel("Phone Number:"));
    JFormattedTextField phoneField = new JFormattedTextField(new MaskFormatter("(###)###-####"));
    phoneField.setValue("(314)888-1234");
    form.add(phoneField);

    JFrame frame = new JFrame("User Information");
    frame.getContentPane().add(form);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:jat.examples.TwoBodyExample.TwoBodyExample.java

public static void main(String[] args) {

    TwoBodyExample x = new TwoBodyExample();

    // create a TwoBody orbit using orbit elements
    TwoBodyAPL sat = new TwoBodyAPL(7000.0, 0.3, 0.0, 0.0, 0.0, 0.0);

    double[] y = sat.randv();

    ArrayRealVector v = new ArrayRealVector(y);

    DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00");
    RealVectorFormat format = new RealVectorFormat(df2);
    System.out.println(format.format(v));

    // find out the period of the orbit
    double period = sat.period();

    // set the final time = one orbit period
    double tf = period;

    // set the initial time to zero
    double t0 = 0.0;

    // propagate the orbit
    FirstOrderIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
    dp853.addStepHandler(sat.stepHandler);
    // double[] y = new double[] { 7000.0, 0, 0, .0, 8, 0 }; // initial
    // state/*from  ww  w  . j  a v a2 s .  c o m*/

    dp853.integrate(sat, 0.0, y, 8000, y); // now y contains final state at
    // tf

    Double[] objArray = sat.time.toArray(new Double[sat.time.size()]);
    double[] timeArray = ArrayUtils.toPrimitive(objArray);
    double[] xsolArray = ArrayUtils.toPrimitive(sat.xsol.toArray(new Double[sat.time.size()]));
    double[] ysolArray = ArrayUtils.toPrimitive(sat.ysol.toArray(new Double[sat.time.size()]));

    double[][] XY = new double[timeArray.length][2];

    // int a=0;
    // System.arraycopy(timeArray,0,XY[a],0,timeArray.length);
    // System.arraycopy(ysolArray,0,XY[1],0,ysolArray.length);

    for (int i = 0; i < timeArray.length; i++) {
        XY[i][0] = xsolArray[i];
        XY[i][1] = ysolArray[i];
    }

    Plot2DPanel p = new Plot2DPanel();

    // Plot2DPanel p = new Plot2DPanel(min, max, axesScales, axesLabels);

    ScatterPlot s = new ScatterPlot("orbit", Color.RED, XY);
    // LinePlot l = new LinePlot("sin", Color.RED, XY);
    // l.closed_curve = false;
    // l.draw_dot = true;
    p.addPlot(s);
    p.setLegendOrientation(PlotPanel.SOUTH);
    double plotSize = 10000.;
    double[] min = { -plotSize, -plotSize };
    double[] max = { plotSize, plotSize };
    p.setFixedBounds(min, max);

    new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    System.out.println("end");

}

From source file:FPAdder.java

public static void main(String[] args) {

    int numArgs = args.length;

    //this program requires at least two arguments on the command line
    if (numArgs < 2) {
        System.out.println("This program requires two command-line arguments.");
    } else {/*w  w w. j a v  a2  s  .c  o m*/
        double sum = 0.0;

        for (int i = 0; i < numArgs; i++) {
            sum += Double.valueOf(args[i]).doubleValue();
        }

        //format the sum
        DecimalFormat myFormatter = new DecimalFormat("###,###.##");
        String output = myFormatter.format(sum);

        //print the sum
        System.out.println(output);
    }
}

From source file:net.morphbank.webclient.ProcessFiles.java

/**
 * @param args/*from   w ww  .jav  a 2s .co  m*/
 *            args[0] directory including terminal '/' 
 *            args[1] prefix of request files 
 *            args[2] number of digits in file index value
 *            args[3] number of files 
 *            args[4] index of first file (default 0) 
 *            args[5] prefix of service 
 *            args[6] prefix of response files (default args[1] + "Resp")
 */
public static void main(String[] args) {
    ProcessFiles fileProcessor = new ProcessFiles();
    // restTest.processRequest(URL, UPLOAD_FILE);

    String zeros = "0000000";

    if (args.length < 4) {
        System.out.println("Too few parameters");
    } else {
        try {
            // get parameters
            String reqDir = args[0];
            String reqPrefix = args[1];
            int numDigits = Integer.valueOf(args[2]);
            if (numDigits > zeros.length())
                numDigits = zeros.length();
            NumberFormat intFormat = new DecimalFormat(zeros.substring(0, numDigits));
            int numFiles = Integer.valueOf(args[3]);
            int firstFile = 0;

            BufferedReader fileIn = new BufferedReader(new FileReader(FILE_IN_PATH));
            String line = fileIn.readLine();
            fileIn.close();
            firstFile = Integer.valueOf(line);
            // firstFile = 189;
            // numFiles = 1;
            int lastFile = firstFile + numFiles - 1;
            String url = URL;
            String respPrefix = reqPrefix + "Resp";
            if (args.length > 5)
                respPrefix = args[5];
            if (args.length > 6)
                url = args[6];

            // process files
            for (int i = firstFile; i <= lastFile; i++) {
                String xmlOutputFile = null;
                String requestFile = reqDir + reqPrefix + intFormat.format(i) + ".xml";
                System.out.println("Processing request file " + requestFile);
                String responseFile = reqDir + respPrefix + intFormat.format(i) + ".xml";
                System.out.println("Response file " + responseFile);
                // restTest.processRequest(URL, UPLOAD_FILE);
                fileProcessor.processRequest(url, requestFile, responseFile);
                Writer fileOut = new FileWriter(FILE_IN_PATH, false);
                fileOut.append(Integer.toString(i + 1));
                fileOut.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}