Example usage for java.text NumberFormat getNumberInstance

List of usage examples for java.text NumberFormat getNumberInstance

Introduction

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

Prototype

public static final NumberFormat getNumberInstance() 

Source Link

Document

Returns a general-purpose number format for the current default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:de.berlios.jhelpdesk.web.manager.ArticleController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(ArticleCategory.class, "category", articleCategoryEditor);
    binder.registerCustomEditor(User.class, "author", userEditor);
    NumberFormat nf = NumberFormat.getNumberInstance();
    binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, nf, true));
}

From source file:org.codekaizen.vtj.text.BpNumberFormatTest.java

/**
 * DOCUMENT ME!//  w  w w . jav  a2 s  . co  m
 */
@Test
public void testFormatting() {
    NumberFormat fmt1 = null;
    NumberFormat fmt2 = null;
    String s1 = null;
    String s2 = null;
    Number num1 = null;

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_NUMBER, null);
    fmt2 = NumberFormat.getNumberInstance();
    num1 = new Integer(25436);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    num1 = new Double(1228.744);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_CURRENCY, null);
    fmt2 = NumberFormat.getCurrencyInstance();
    num1 = new Integer(25436);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    num1 = new Double(1228.744);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_CURRENCY, Locale.CANADA);
    fmt2 = NumberFormat.getCurrencyInstance(Locale.CANADA);
    num1 = new Integer(25436);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    num1 = new Double(1228.744);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_PERCENT, null);
    fmt2 = NumberFormat.getPercentInstance();
    num1 = new Double(0.7444);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

}

From source file:com.mxgraph.examples.swing.chart.PieChartDemo1.java

/**
 * Creates a chart./*from   w  w  w  . j a v  a2 s .c  o m*/
 *
 * @param dataset  the dataset.
 *
 * @return A chart.
 */
public static JFreeChart createChart(PieDataset dataset) {

    JFreeChart chart = ChartFactory.createPieChart("", // chart title
            dataset, // data
            true, // include legend
            true, false);

    PiePlot plot = (PiePlot) chart.getPlot();

    plot.setSectionOutlinesVisible(false);
    plot.setNoDataMessage("No data available");

    plot.setCircular(true);
    plot.setLabelGap(0.01D);//
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:\r\n{1}({2})",
            NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); //  //
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));

    chart.getTitle().setFont(new Font("", Font.PLAIN, 20));//
    PiePlot piePlot = (PiePlot) chart.getPlot();//
    piePlot.setLabelFont(new Font("", Font.PLAIN, 10));
    chart.getLegend().setItemFont(new Font("", Font.PLAIN, 10));

    return chart;

}

From source file:de.berlios.jhelpdesk.web.manager.users.UserEditController.java

/**
 * Rejestruje edytory waciwoci niezbdne podczas edycji danych
 * uytkownika.//from   w w  w  . j a  va  2s.c om
 *
 * @param binder
 *
 * @see WebDataBinder
 */
@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Role.class, "userRole", roleEditor);
    binder.registerCustomEditor(Long.class, null,
            new CustomNumberEditor(Long.class, NumberFormat.getNumberInstance(), true));
    binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
}

From source file:components.ConversionPanel.java

ConversionPanel(Converter myController, String myTitle, Unit[] myUnits, ConverterRangeModel myModel) {
    if (MULTICOLORED) {
        setOpaque(true);/*from  ww w  .  j a  v a 2s  .c o  m*/
        setBackground(new Color(0, 255, 255));
    }
    setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(myTitle),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Save arguments in instance variables.
    controller = myController;
    units = myUnits;
    title = myTitle;
    sliderModel = myModel;

    //Create the text field format, and then the text field.
    numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMaximumFractionDigits(2);
    NumberFormatter formatter = new NumberFormatter(numberFormat);
    formatter.setAllowsInvalid(false);
    formatter.setCommitsOnValidEdit(true);//seems to be a no-op --
    //aha -- it changes the value property but doesn't cause the result to
    //be parsed (that happens on focus loss/return, I think).
    //
    textField = new JFormattedTextField(formatter);
    textField.setColumns(10);
    textField.setValue(new Double(sliderModel.getDoubleValue()));
    textField.addPropertyChangeListener(this);

    //Add the combo box.
    unitChooser = new JComboBox();
    for (int i = 0; i < units.length; i++) { //Populate it.
        unitChooser.addItem(units[i].description);
    }
    unitChooser.setSelectedIndex(0);
    sliderModel.setMultiplier(units[0].multiplier);
    unitChooser.addActionListener(this);

    //Add the slider.
    slider = new JSlider(sliderModel);
    sliderModel.addChangeListener(this);

    //Make the text field/slider group a fixed size
    //to make stacked ConversionPanels nicely aligned.
    JPanel unitGroup = new JPanel() {
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        public Dimension getPreferredSize() {
            return new Dimension(150, super.getPreferredSize().height);
        }

        public Dimension getMaximumSize() {
            return getPreferredSize();
        }
    };
    unitGroup.setLayout(new BoxLayout(unitGroup, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        unitGroup.setOpaque(true);
        unitGroup.setBackground(new Color(0, 0, 255));
    }
    unitGroup.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
    unitGroup.add(textField);
    unitGroup.add(slider);

    //Create a subpanel so the combo box isn't too tall
    //and is sufficiently wide.
    JPanel chooserPanel = new JPanel();
    chooserPanel.setLayout(new BoxLayout(chooserPanel, BoxLayout.PAGE_AXIS));
    if (MULTICOLORED) {
        chooserPanel.setOpaque(true);
        chooserPanel.setBackground(new Color(255, 0, 255));
    }
    chooserPanel.add(unitChooser);
    chooserPanel.add(Box.createHorizontalStrut(100));

    //Put everything together.
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    add(unitGroup);
    add(chooserPanel);
    unitGroup.setAlignmentY(TOP_ALIGNMENT);
    chooserPanel.setAlignmentY(TOP_ALIGNMENT);
}

From source file:org.apache.hadoop.hive.ql.udf.UDFNumberFormat.java

public String evaluate(DoubleWritable d) {
    if (d == null) {
        return null;
    }/* w w  w. j  a  v  a2 s .  co  m*/
    double ori = d.get();
    try {
        NumberFormat nFormat = NumberFormat.getNumberInstance();
        nFormat.setMaximumFractionDigits(maxdefault);
        nFormat.setMaximumIntegerDigits(maxdefault);
        nFormat.setGroupingUsed(false);
        return nFormat.format(ori);
    } catch (Exception e) {
        LOG.error("can not format value:  " + ori);
        return null;
    }
}

From source file:org.jfree.chart.demo.XMLPieChartDemo.java

/**
 * Default constructor.//  www .j a va2 s .  c o  m
 *
 * @param title  the frame title.
 */
public XMLPieChartDemo(final String title) {

    super(title);

    // create a dataset...
    PieDataset dataset = null;
    final URL url = getClass().getResource("/org/jfree/chart/demo/piedata.xml");

    try {
        final InputStream in = url.openStream();
        dataset = DatasetReader.readPieDatasetFromXML(in);
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    }

    // create the chart...
    final JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart title
            dataset, // data
            true, // include legend
            true, false);

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.yellow);
    final PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelGenerator(new StandardPieItemLabelGenerator("{0} = {2}", NumberFormat.getNumberInstance(),
            NumberFormat.getPercentInstance()));
    plot.setNoDataMessage("No data available");

    // add the chart to a panel...
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);

}

From source file:org.jfree.chart.demo.PieChartDemo4.java

/**
 * Default constructor.//w  w  w.  ja v  a 2 s  .  c o  m
 *
 * @param title  the frame title.
 */
public PieChartDemo4(final String title) {

    super(title);
    final PieDataset dataset = createDataset(14);

    // create the chart...
    final JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 4", // chart title
            dataset, // dataset
            false, // include legend
            true, false);

    // set the background color for the chart...
    chart.setBackgroundPaint(new Color(222, 222, 255));
    final PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setCircular(true);
    plot.setLabelGenerator(new StandardPieItemLabelGenerator("{0} = {2}", NumberFormat.getNumberInstance(),
            NumberFormat.getPercentInstance()));
    plot.setNoDataMessage("No data available");

    // add the chart to a panel...
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);

    final Rotator rotator = new Rotator(plot);
    rotator.start();

}

From source file:net.sf.mzmine.modules.peaklistmethods.dataanalysis.projectionplots.ProjectionPlotPanel.java

public ProjectionPlotPanel(ProjectionPlotWindow masterFrame, ProjectionPlotDataset dataset,
        ParameterSet parameters) {//from   www .  j av a 2s . c  o  m
    super(null);

    boolean createLegend = false;
    if ((dataset.getNumberOfGroups() > 1) && (dataset.getNumberOfGroups() < 20))
        createLegend = true;

    chart = ChartFactory.createXYAreaChart("", dataset.getXLabel(), dataset.getYLabel(), dataset,
            PlotOrientation.VERTICAL, createLegend, false, false);
    chart.setBackgroundPaint(Color.white);

    setChart(chart);

    // title

    TextTitle chartTitle = chart.getTitle();
    chartTitle.setMargin(5, 0, 0, 0);
    chartTitle.setFont(titleFont);
    chart.removeSubtitle(chartTitle);

    // disable maximum size (we don't want scaling)
    setMaximumDrawWidth(Integer.MAX_VALUE);
    setMaximumDrawHeight(Integer.MAX_VALUE);

    // set the plot properties
    plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));

    // set grid properties
    plot.setDomainGridlinePaint(gridColor);
    plot.setRangeGridlinePaint(gridColor);

    // set crosshair (selection) properties
    plot.setDomainCrosshairVisible(false);
    plot.setRangeCrosshairVisible(false);

    plot.setForegroundAlpha(dataPointAlpha);

    NumberFormat numberFormat = NumberFormat.getNumberInstance();

    // set the X axis (component 1) properties
    NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
    xAxis.setNumberFormatOverride(numberFormat);

    // set the Y axis (component 2) properties
    NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
    yAxis.setNumberFormatOverride(numberFormat);

    plot.setDataset(dataset);

    spotRenderer = new ProjectionPlotRenderer(plot, dataset);
    itemLabelGenerator = new ProjectionPlotItemLabelGenerator(parameters);
    spotRenderer.setBaseItemLabelGenerator(itemLabelGenerator);
    spotRenderer.setBaseItemLabelsVisible(true);
    spotRenderer.setBaseToolTipGenerator(new ProjectionPlotToolTipGenerator(parameters));
    plot.setRenderer(spotRenderer);

    // Setup legend
    if (createLegend) {
        LegendItemCollection legendItemsCollection = new LegendItemCollection();
        for (int groupNumber = 0; groupNumber < dataset.getNumberOfGroups(); groupNumber++) {
            Object paramValue = dataset.getGroupParameterValue(groupNumber);
            if (paramValue == null) {
                // No parameter value available: search for raw data files
                // within this group, and use their names as group's name
                String fileNames = new String();
                for (int itemNumber = 0; itemNumber < dataset.getItemCount(0); itemNumber++) {
                    String rawDataFile = dataset.getRawDataFile(itemNumber);
                    if (dataset.getGroupNumber(itemNumber) == groupNumber)
                        fileNames = fileNames.concat(rawDataFile);
                }
                if (fileNames.length() == 0)
                    fileNames = "Empty group";

                paramValue = fileNames;
            }
            Color nextColor = (Color) spotRenderer.getGroupPaint(groupNumber);
            Color groupColor = new Color(nextColor.getRed(), nextColor.getGreen(), nextColor.getBlue(),
                    (int) Math.round(255 * dataPointAlpha));
            legendItemsCollection.add(new LegendItem(paramValue.toString(), "-", null, null,
                    spotRenderer.getDataPointsShape(), groupColor));
        }
        plot.setFixedLegendItems(legendItemsCollection);
    }

}

From source file:cn.edu.thss.iise.bpmdemo.charts.PieChartDemo4.java

/**
 * Default constructor.//from  w  w  w.j a v  a  2 s.  c om
 *
 * @param title
 *            the frame title.
 */
public PieChartDemo4(final String title) {

    super(title);
    final PieDataset dataset = createDataset(14);

    // create the chart...
    final JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 4", // chart title
            dataset, // dataset
            false, // include legend
            true, false);

    // set the background color for the chart...
    chart.setBackgroundPaint(new Color(222, 222, 255));

    final PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setCircular(true);
    plot.setLabelGenerator(new StandardPieItemLabelGenerator("{0} = {2}", NumberFormat.getNumberInstance(),
            NumberFormat.getPercentInstance()));
    plot.setNoDataMessage("No data available");

    // add the chart to a panel...
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

    // add table
    Object[][] data = { { "Kathy0", new Integer(0) }, { "Kathy1", new Integer(2) },
            { "Kathy2", new Integer(5) }, { "Kathy3", new Integer(5) }, { "Kathy4", new Integer(5) },
            { "Kathy5", new Integer(5) }, { "Kathy6", new Integer(5) }, { "Kathy7", new Integer(5) },
            { "Kathy8", new Integer(5) }, { "Kathy9", new Integer(5) } };
    String[] columnNames = { "Model Name", "Reuse Number" };
    JTable table = new JTable(data, columnNames);
    final JPanel panel = new JPanel();
    panel.add(chartPanel);
    panel.add(table);
    setContentPane(panel);
    final Rotator rotator = new Rotator(plot);
    rotator.start();

}