Example usage for java.text DecimalFormat setGroupingUsed

List of usage examples for java.text DecimalFormat setGroupingUsed

Introduction

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

Prototype

@Override
public void setGroupingUsed(boolean newValue) 

Source Link

Usage

From source file:NumericTextField.java

public static void main(String[] args) {
    try {//w  w w  . j  av  a2  s  .  c  om
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    DecimalFormat format = new DecimalFormat("#,###.###");
    format.setGroupingUsed(true);
    format.setGroupingSize(3);
    format.setParseIntegerOnly(false);

    JFrame f = new JFrame("Numeric Text Field Example");
    final NumericTextField tf = new NumericTextField(10, format);

    tf.setValue((double) 123456.789);

    JLabel lbl = new JLabel("Type a number: ");
    f.getContentPane().add(tf, "East");
    f.getContentPane().add(lbl, "West");

    tf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            try {
                tf.normalize();
                Long l = tf.getLongValue();
                System.out.println("Value is (Long)" + l);
            } catch (ParseException e1) {
                try {
                    Double d = tf.getDoubleValue();
                    System.out.println("Value is (Double)" + d);
                } catch (ParseException e2) {
                    System.out.println(e2);
                }
            }
        }
    });
    f.pack();
    f.setVisible(true);
}

From source file:org.toobsframework.transformpipeline.xslExtentions.PriceFormatHelper.java

/**
 * Gets a string that represents the input Price formatted into the proper fromate, and converted
 * into the proper timezone.//from   w  ww .j a  v  a 2s  .  c o  m
 *
 * @return Price-only string formatted with given time zone.
 *
 * @exception XMLTransfromerException if parsing problem occurs
 */
public static String getFormattedPrice(String inputPrice, String priceFormat, String language)
        throws XMLTransformerException {
    if ((inputPrice == null) || (inputPrice.trim().length() == 0)) {
        inputPrice = "0";
    }

    Locale locale = new Locale(language.substring(2, 4).toLowerCase(), language.substring(0, 2));
    DecimalFormat priceFormatter = (DecimalFormat) NumberFormat.getNumberInstance(locale);
    priceFormatter.setGroupingUsed(true);
    priceFormatter.setMaximumFractionDigits(2);
    priceFormatter.setMinimumFractionDigits(2);

    priceFormatter.applyPattern(priceFormat);

    return priceFormatter.format(new Double(inputPrice));
}

From source file:org.sakaiproject.tool.assessment.jsf.validator.FinQuestionValidator.java

static boolean isComplexNumber(String value) {

    boolean isComplex = true;
    Complex complex = null;/*w  w w .  j  a v  a2s  . com*/
    try {
        DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
        df.setGroupingUsed(false);

        // Numerical format ###.## (decimal symbol is the point)
        ComplexFormat complexFormat = new ComplexFormat(df);
        complex = complexFormat.parse(value);

        // This is because there is a bug parsing complex number. 9i is parsed as 9
        if (complex.getImaginary() == 0 && value.contains("i"))
            isComplex = false;
    } catch (Exception e) {
        isComplex = false;
    }

    return isComplex;
}

From source file:pl.otros.vfs.browser.table.FileSize.java

private static String format(final long value, final long divider, final String unit) {
    final double result = divider > 1 ? (double) value / (double) divider : (double) value;
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setMaximumFractionDigits(1);
    decimalFormat.setMinimumFractionDigits(0);
    decimalFormat.setGroupingUsed(false);
    decimalFormat.setDecimalSeparatorAlwaysShown(false);
    return decimalFormat.format(result) + " " + unit;
}

From source file:org.locationtech.udig.tools.internal.CursorPosition.java

private static String getString(double value) {
    if (Double.isNaN(value)) {
        return Messages.CursorPosition_not_a_number;
    }// w  ww .j ava  2  s  .c  o  m

    if (Double.isInfinite(value)) {
        return Messages.CursorPosition_infinity;
    }

    DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
    format.setMaximumFractionDigits(4);
    format.setMinimumIntegerDigits(1);
    format.setGroupingUsed(false);
    String string = format.format(value);

    String[] parts = string.split("\\.");
    if (parts.length > 3) {
        string = parts[0];
    }
    return string;
}

From source file:Main.java

public static DecimalFormat createDecimalFormat(int minInt, int maxInt, int minFract, int maxFract,
        char separator, RoundingMode mode) {

    DecimalFormat format = (DecimalFormat) DecimalFormat.getNumberInstance();
    format.setRoundingMode(mode);/*from w  ww . j a v a  2  s  .c o  m*/

    format.setMaximumFractionDigits(maxFract);
    format.setMinimumFractionDigits(minFract);
    format.setMaximumIntegerDigits(maxInt);
    format.setMinimumIntegerDigits(minInt);
    DecimalFormatSymbols decimalSymbolComma = new DecimalFormatSymbols();
    decimalSymbolComma.setDecimalSeparator(separator);
    format.setDecimalFormatSymbols(decimalSymbolComma);
    format.setGroupingUsed(false);

    return format;
}

From source file:com.itude.mobile.mobbl.core.util.StringUtilities.java

public static String formatVolume(String stringToFormat) {
    if (stringToFormat == null || stringToFormat.length() == 0) {
        return null;
    }/*  w  ww  .j  av  a2 s  .c  o  m*/

    String result = null;

    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(getDefaultFormattingLocale()));

    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
    formatter.setMaximumFractionDigits(0);

    result = formatter.format(Double.parseDouble(stringToFormat));

    return result;
}

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

public static String summarizeDetailedOccupancyStats(Map<Id<Vehicle>, double[]> vehicleDistances, String del,
        int maxcap) {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);/*from w  w w . j av  a  2 s . c o  m*/
    format.setMaximumFractionDigits(2);
    format.setGroupingUsed(false);

    double[] sum = new double[maxcap + 1];

    for (double[] dist : vehicleDistances.values()) {
        double emptyD = dist[0] - dist[2];
        sum[0] += emptyD;
        for (int i = 3; i < maxcap + 3; i++) {
            sum[i - 2] += dist[i];
        }
    }
    String result = "";
    for (int i = 0; i <= maxcap; i++) {
        result = result + ";" + format.format(sum[i]);
    }

    return result;
}

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

/**
 * @param vehicleDistances/*from w  w  w  .j  a  v  a 2 s. c  o  m*/
 * @param string
 * @return
 */
public static String summarizeVehicles(Map<Id<Vehicle>, double[]> vehicleDistances, String del) {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(2);
    format.setGroupingUsed(false);

    DescriptiveStatistics driven = new DescriptiveStatistics();
    DescriptiveStatistics revenue = new DescriptiveStatistics();
    DescriptiveStatistics occupied = new DescriptiveStatistics();
    DescriptiveStatistics empty = new DescriptiveStatistics();

    for (double[] dist : vehicleDistances.values()) {
        driven.addValue(dist[0]);
        revenue.addValue(dist[1]);
        occupied.addValue(dist[2]);
        double emptyD = dist[0] - dist[2];
        empty.addValue(emptyD);
    }
    double d_r_d_t = revenue.getSum() / driven.getSum();
    // bw.write("iteration;vehicles;totalDistance;totalEmptyDistance;emptyRatio;totalRevenueDistance;averageDrivenDistance;averageEmptyDistance;averageRevenueDistance");
    String result = vehicleDistances.size() + del + format.format(driven.getSum()) + del
            + format.format(empty.getSum()) + del + format.format(empty.getSum() / driven.getSum()) + del
            + format.format(revenue.getSum()) + del + format.format(driven.getMean()) + del
            + format.format(empty.getMean()) + del + format.format(revenue.getMean()) + del
            + format.format(d_r_d_t);
    return result;
}

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

/**
 * @param vehicleDistances// www. ja va  2  s .  c o  m
 * @param iterationFilename
 */
public static void writeVehicleDistances(Map<Id<Vehicle>, double[]> vehicleDistances,
        String iterationFilename) {
    String header = "vehicleId;drivenDistance_m;occupiedDistance_m;emptyDistance_m;revenueDistance_pm";
    BufferedWriter bw = IOUtils.getBufferedWriter(iterationFilename);
    DecimalFormat format = new DecimalFormat();
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    format.setMinimumIntegerDigits(1);
    format.setMaximumFractionDigits(2);
    format.setGroupingUsed(false);
    try {
        bw.write(header);
        for (Entry<Id<Vehicle>, double[]> e : vehicleDistances.entrySet()) {
            double drivenDistance = e.getValue()[0];
            double revenueDistance = e.getValue()[1];
            double occDistance = e.getValue()[2];
            double emptyDistance = drivenDistance - occDistance;
            bw.newLine();
            bw.write(e.getKey().toString() + ";" + format.format(drivenDistance) + ";"
                    + format.format(occDistance) + ";" + format.format(emptyDistance) + ";"
                    + format.format(revenueDistance));
        }
        bw.flush();
        bw.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}