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:FormatUtils.java

public static String formatDouble(Object value) {
    NumberFormat doubleFormat = NumberFormat.getNumberInstance();

    return doubleFormat.format(((Number) value).doubleValue());
}

From source file:Main.java

public static int getMatchingThresholdFromString(String value) throws ParseException {
    char percent = new DecimalFormatSymbols().getPercent();
    value = value.replace(percent, ' ');
    Number number = NumberFormat.getNumberInstance().parse(value);
    double parse = number.doubleValue();
    double p = Math.log10(Math.max(Double.MIN_VALUE, Math.min(1, parse / 100)));
    return Math.max(0, (int) Math.round(-12 * p));
}

From source file:Utilities.java

public static String formatSize(long longSize, int decimalPos) {
    NumberFormat fmt = NumberFormat.getNumberInstance();
    if (decimalPos >= 0) {
        fmt.setMaximumFractionDigits(decimalPos);
    }/*from  ww w  . jav  a  2s  .c om*/
    final double size = longSize;
    double val = size / (1024 * 1024);
    if (val > 1) {
        return fmt.format(val).concat(" MB");
    }
    val = size / 1024;
    if (val > 10) {
        return fmt.format(val).concat(" KB");
    }
    return fmt.format(val).concat(" bytes");
}

From source file:Main.java

/**
 * Format the amount in template transaction splits.
 * <p>GnuCash desktop always formats with a locale dependent format, and that varies per user.<br>
 * So we will use the device locale here and hope that the user has the same locale on the desktop GnuCash</p>
 * @param amount Amount to be formatted//from  www .  j a  v a2  s .  c o  m
 * @return String representation of amount
 */
public static String formatTemplateSplitAmount(BigDecimal amount) {
    //TODO: If we ever implement an application-specific locale setting, use it here as well
    return NumberFormat.getNumberInstance().format(amount);
}

From source file:com.doculibre.constellio.utils.FileSizeUtils.java

public static String formatSize(long fileSize, int decimalPos) {
    NumberFormat fmt = NumberFormat.getNumberInstance();
    if (decimalPos >= 0) {
        fmt.setMaximumFractionDigits(decimalPos);
    }//w  w w.j  av a2  s.  co m
    final double size = fileSize;
    double val = size / (1024 * 1024);
    if (val > 1) {
        return fmt.format(val).concat(" MB");
    }
    val = size / 1024;
    if (val > 10) {
        return fmt.format(val).concat(" KB");
    }
    return fmt.format(val).concat(" bytes");
}

From source file:com.example.awesomedogs.util.Stuff.java

/**
 * Converts some currency cents to a more readable format.
 * //from   w w w  .j av a  2s.  c  o  m
 * @param cents Amount in cents, e.g. 123.
 * @return Formatted value, e.g. "1.23".
 */
public static String formatCents(int cents) {
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    return nf.format(((double) cents / 100));
}

From source file:funcoes.funcoes.java

public static String formatoParaInserir(String valor) throws ParseException {

    NumberFormat formato2 = NumberFormat.getNumberInstance();
    return formato2.parse(valor).toString();

}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    amountDisplayFormat = NumberFormat.getCurrencyInstance();
    System.out.println(amountDisplayFormat.format(1200));
    amountDisplayFormat.setMinimumFractionDigits(0);
    amountEditFormat = NumberFormat.getNumberInstance();
    amountField = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(amountDisplayFormat),
            new NumberFormatter(amountDisplayFormat), new NumberFormatter(amountEditFormat)));
    amountField.setValue(new Double(amount));
    amountField.setColumns(10);//from w w  w  .  j  a  va 2  s.  co m
    amountField.addPropertyChangeListener("value", this);

    JPanel fieldPane = new JPanel(new GridLayout(0, 1));
    fieldPane.add(amountField);

    add(fieldPane, BorderLayout.LINE_END);
    add(new JButton("Hello"), BorderLayout.SOUTH);
}

From source file:Main.java

public Main() {
    formTextFieldFormat = NumberFormat.getNumberInstance();
    formTextFieldFormat.setMinimumFractionDigits(2);
    formTextFieldFormat.setMaximumFractionDigits(2);
    formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

    focusLabel.setPreferredSize(new Dimension(120, 27));
    formTextField = new JFormattedTextField(formTextFieldFormat);
    formTextField.setValue(amount);//from w w  w. j ava  2 s .  c  om
    formTextField.setPreferredSize(new Dimension(120, 27));
    formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField.requestFocus();
            formTextField.setText(formTextField.getText());
            formTextField.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
            double t1a1 = (((Number) formTextField.getValue()).doubleValue());
            if (t1a1 < 1000) {
                formTextField.setValue(amount);
            }
        }
    });

    docLabel.setPreferredSize(new Dimension(120, 27));

    formTextField1 = new JFormattedTextField(formTextFieldFormat);
    formTextField1.setValue(amount);

    formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField1.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField1.requestFocus();
            formTextField1.setText(formTextField1.getText());
            formTextField1.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
        }
    });
    formTextField1.getDocument().addDocumentListener(docListener);

    pnl = new JPanel();
    pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
    pnl.setLayout(new GridLayout(2, 2));
    pnl.add(focusLabel);
    pnl.add(formTextField);
    pnl.add(docLabel);
    pnl.add(formTextField1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pnl, BorderLayout.CENTER);
    frame.setLocation(200, 200);
    frame.pack();
    frame.setVisible(true);
    formTextFieldFocus1();
}

From source file:com.globalsight.util.JfreeCharUtil.java

public static void drawPieChart2D(String title, Map<String, Double> datas, File OutFile) {
    PieDataset dataset = buildDatas(datas);
    JFreeChart chart = ChartFactory.createPieChart(title, dataset, true, true, false);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})",
            NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
    chart.setBackgroundPaint(Color.white);
    plot.setCircular(true);/*w w  w  .  j  av a  2 s . c o  m*/
    TextTitle textTitle = new TextTitle(title);
    Font font = new Font(textTitle.getFont().getName(), Font.CENTER_BASELINE, 20);
    textTitle.setFont(font);
    chart.setTitle(textTitle);
    FileOutputStream fos_jpg = null;

    try {
        fos_jpg = new FileOutputStream(OutFile);
        ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 640, 480, null);
        fos_jpg.close();
    } catch (Exception e) {
        s_logger.error(e.getMessage(), e);
    }
}