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:thymeleafsandbox.springjsp.web.conversion.NumberFormatter.java

public String print(final Number object, final Locale locale) {
    final DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getNumberInstance();
    final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
    symbols.setGroupingSeparator('*');
    numberFormat.setDecimalFormatSymbols(symbols);
    return numberFormat.format(object);
}

From source file:utils.Graphs.java

public static File generate(File baseFolder, String titles[], int[] values, Color backgroundColor) {
    DefaultPieDataset dataset = new DefaultPieDataset();

    // add our data values
    int i = -1;/*from  w  ww  . j av a 2s.c o m*/
    for (String title : titles) {
        i++;
        dataset.setValue(title, values[i]);
    }

    final JFreeChart chart =
            //                ChartFactory.createPieChart("", dataset, true, true, false);

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

    PiePlot plot = (PiePlot) chart.getPlot();
    //PiePlot3D plot = (PiePlot3D) chart.getPlot();
    //plot.setStartAngle(290);
    plot.setStartAngle(45);
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setForegroundAlpha(0.5f);

    //        final PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(backgroundColor);

    //        plot.setLegendLabelGenerator(
    //        new StandardPieSectionLabelGenerator("{0} {2}"));

    chart.setBorderVisible(false);
    chart.getPlot().setOutlineVisible(false);
    chart.getLegend().setFrame(BlockBorder.NONE);

    // get the same background
    chart.setBackgroundPaint(backgroundColor);
    chart.getLegend().setBackgroundPaint(backgroundColor);

    // hide the shadow effects
    plot.setShadowXOffset(0);
    plot.setShadowYOffset(0);

    //chart.getLegend().setVisible(false);

    plot.setCircular(true);
    //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {2}", NumberFormat.getNumberInstance(), NumberFormat
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}", NumberFormat.getNumberInstance(),
            NumberFormat.getPercentInstance()));
    plot.setNoDataMessage("No data found.");

    Color greenColor = new Color(0x8FBC0C);
    Color redColor = new Color(0xFF0000);
    //Color redColor = new Color(0xDA6022);

    plot.setSectionPaint(0, greenColor);
    plot.setSectionPaint(1, redColor);
    plot.setSectionOutlinesVisible(true);

    final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
    final File file = new File(baseFolder, "chart.png");
    try {
        ChartUtilities.saveChartAsPNG(file, chart, 200, 160, info);
    } catch (IOException ex) {
        Logger.getLogger(Graphs.class.getName()).log(Level.SEVERE, null, ex);
    }
    //        
    ////        final ChartPanel chartPanel = new ChartPanel(chart, true);
    //       final ChartPanel chartPanel = new ChartPanel(chart, true);
    //        chartPanel.setMinimumDrawWidth(0);
    //        chartPanel.setMaximumDrawWidth(Integer.MAX_VALUE);
    //        chartPanel.setMinimumDrawHeight(0);
    //        chartPanel.setMaximumDrawHeight(Integer.MAX_VALUE);
    //        JDialog dialog = new JDialog();
    //        dialog.add(chartPanel);
    //        dialog.setLayout(new GridLayout(1, 1));
    //        dialog.setSize(400, 200);
    //        dialog.setVisible(true);

    return file;
}

From source file:com.asburymotors.android.disneysocal.common.Utils.java

/**
 * Calculate distance between two LatLng points and format it nicely for
 * display. As this is a sample, it only statically supports metric units.
 * A production app should check locale and support the correct units.
 *//*w  w w  .ja v a2 s.  com*/
public static String formatDistanceBetween(LatLng point1, LatLng point2) {
    if (point1 == null || point2 == null) {
        return null;
    }

    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    double distance = Math.round(SphericalUtil.computeDistanceBetween(point1, point2));

    // Adjust to KM if M goes over 1000 (see javadoc of method for note
    // on only supporting metric)
    if (distance >= 1000) {
        numberFormat.setMaximumFractionDigits(1);
        return numberFormat.format(distance / 1000) + DISTANCE_KM_POSTFIX;
    }
    return numberFormat.format(distance) + DISTANCE_M_POSTFIX;
}

From source file:org.jactr.eclipse.runtime.debug.elements.ACTRStackFrame.java

public ACTRStackFrame(ACTRThread thread, double time, String productionName, int lineNumber) {
    _time = time;/* w  ww .j  a v a2s .co  m*/
    _productionName = productionName;
    _line = lineNumber;
    _thread = thread;

    StringBuilder sb = new StringBuilder();
    sb.append("[");
    sb.append(NumberFormat.getNumberInstance().format(_time)).append("] : ");
    sb.append(_productionName);
    _displayText = sb.toString();
    setDebugTarget((ACTRDebugTarget) thread.getDebugTarget());
}

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

public static void drawPieChart3D(String title, Map<String, Double> datas, File OutFile) {
    PieDataset dataset = buildDatas(datas);
    JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true, true, false);
    PiePlot3D plot = (PiePlot3D) 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.setForegroundAlpha(0.7f);//from  w ww. ja  va 2s  .  c  o  m
    plot.setCircular(true);
    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);
    }
}

From source file:de.berlios.jhelpdesk.web.manager.ticketcategory.TicketCategoryEditController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Long.class, null,
            new CustomNumberEditor(Long.class, NumberFormat.getNumberInstance(), true));
    binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
}

From source file:com.newatlanta.bluedragon.XYItemLabelGenerator.java

/**
 * Creates an item label generator using default number formatters.
 *///from   ww  w . ja v  a  2s . c  o m
public XYItemLabelGenerator() {
    this(DEFAULT_ITEM_LABEL_FORMAT, NumberFormat.getNumberInstance(), NumberFormat.getNumberInstance());
}

From source file:org.kuali.kra.infrastructure.BudgetDecimalFormatter.java

/**
 * Returns a string representation of its argument formatted as a currency value.
 *//*from  w w w  .j  a  v  a  2  s. com*/
public Object format(Object obj) {

    if (LOG.isTraceEnabled()) {
        LOG.trace("format '" + obj + "'");
    }

    if (obj == null)
        return null;

    //NumberFormat formatter = NumberFormat.getCurrencyInstance();
    NumberFormat formatter = NumberFormat.getNumberInstance();
    //if (formatter instanceof DecimalFormat) {
    ((DecimalFormat) formatter).setParseBigDecimal(true);
    ((DecimalFormat) formatter).setDecimalSeparatorAlwaysShown(true);
    //}
    String string = null;

    try {
        BudgetDecimal number = (BudgetDecimal) obj;
        string = formatter.format(number.doubleValue());
    } catch (IllegalArgumentException e) {
        throw new FormatException("formatting", RiceKeyConstants.ERROR_BIG_DECIMAL, obj.toString(), e);
    } catch (ClassCastException e) {
        throw new FormatException("formatting", RiceKeyConstants.ERROR_BIG_DECIMAL, obj.toString(), e);
    }

    if (obj.toString().length() > 15) {
        return obj.toString();
    }
    if (StringUtils.isNotBlank(string)) {
        if (string.indexOf(".") == string.length() - 1) {
            string = string + "00";
        } else if (string.indexOf(".") == string.length() - 2) {
            string = string + "0";
        }
    }
    return string;
}

From source file:com.haulmont.chile.core.datatypes.impl.NumberDatatype.java

/**
 * Creates non-localized format.// w  w  w .j  a va 2  s.com
 */
protected NumberFormat createFormat() {
    if (formatPattern != null) {
        DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();

        if (!StringUtils.isBlank(decimalSeparator))
            formatSymbols.setDecimalSeparator(decimalSeparator.charAt(0));

        if (!StringUtils.isBlank(groupingSeparator))
            formatSymbols.setGroupingSeparator(groupingSeparator.charAt(0));

        return new DecimalFormat(formatPattern, formatSymbols);
    } else {
        return NumberFormat.getNumberInstance();
    }
}

From source file:no.abmu.abmstatistikk.annualstatistic.service.PasientBibValidator.java

public void validate(Object obj, Errors errors) {
    NumberFormat fieldNameFormater = NumberFormat.getNumberInstance();
    fieldNameFormater.setMinimumIntegerDigits(3);
    Set fieldKeys = new HashSet();
    int i;/* w w  w  .  j a v  a  2 s.  co m*/
    setSchemaName("pasientBibSchema");

    switch (pageNumber) {
    case 1:
        break;
    case 2:
        for (i = 13; i <= 14; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 16; i <= 17; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 19; i <= 20; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 22; i <= 23; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 31; i <= 32; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 34; i <= 35; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 37; i <= 38; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 40; i <= 41; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 46; i <= 47; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 49; i <= 50; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 58; i <= 59; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 385; i <= 386; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        break;
    case 3:
        for (i = 67; i <= 77; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 81; i <= 82; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }

        fieldKeys.add(fieldNameFormater.format((long) 82));
        for (i = 84; i <= 87; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        fieldKeys.add(fieldNameFormater.format((long) 90));
        for (i = 97; i <= 101; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 389; i <= 390; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 431; i <= 452; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        break;
    case 4:
        for (i = 102; i <= 107; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 117; i <= 119; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        fieldKeys.add(fieldNameFormater.format((long) 147));
        for (i = 151; i <= 160; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        fieldKeys.add(fieldNameFormater.format((long) 162));
        for (i = 205; i <= 210; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 223; i <= 224; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        for (i = 401; i <= 402; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        break;
    case 5:
        fieldKeys.add(fieldNameFormater.format((long) 243));
        fieldKeys.add(fieldNameFormater.format((long) 246));
        fieldKeys.add(fieldNameFormater.format((long) 249));
        fieldKeys.add(fieldNameFormater.format((long) 252));
        for (i = 259; i <= 264; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        fieldKeys.add(fieldNameFormater.format((long) 266));
        fieldKeys.add(fieldNameFormater.format((long) 372));
        for (i = 381; i <= 382; i++) {
            fieldKeys.add(fieldNameFormater.format((long) i));
        }
        break;
    default:
        break;

    }

    logger.debug("Keys to validate: " + fieldKeys);
    setValidateFieldKeys(fieldKeys);
    super.validate(obj, errors);
}