Example usage for java.text DecimalFormatSymbols DecimalFormatSymbols

List of usage examples for java.text DecimalFormatSymbols DecimalFormatSymbols

Introduction

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

Prototype

public DecimalFormatSymbols() 

Source Link

Document

Create a DecimalFormatSymbols object for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java

/**
 * Parses a value into a java Number (BigDecimal) using the comma for decimal separator.
 * @param amount to parse//from   www.java  2  s. c  o m
 * @return Number of the parsed amount or <code>null</code> if the number could not be parsed
 */
public static Number getNumber(final String amount) {
    Number number = null;
    if (amount != null) {
        try {
            final DecimalFormatSymbols symbols = new DecimalFormatSymbols();
            symbols.setDecimalSeparator(',');
            final DecimalFormat df = new DecimalFormat("00.##", symbols);
            df.setParseBigDecimal(true);
            number = df.parse(amount);
        } catch (final ParseException e) {
            log.log(java.util.logging.Level.WARNING, "Error parsing number", e);
        }
    }
    return number;
}

From source file:com.epam.dlab.core.parser.CommonFormat.java

/**
 * Create and return decimal formatter./*from  w w  w . j a  v  a2 s  .c om*/
 *
 * @param decimalSeparator  the character used for decimal sign.
 * @param groupingSeparator the character used for thousands separator.
 * @return Formatter for decimal digits.
 */
private DecimalFormat getDecimalFormat(char decimalSeparator, char groupingSeparator) {
    DecimalFormat df = new DecimalFormat();
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator(decimalSeparator);
    symbols.setGroupingSeparator(groupingSeparator);
    df.setDecimalFormatSymbols(symbols);
    return df;
}

From source file:org.dashbuilder.dataprovider.backend.csv.CSVParser.java

protected DecimalFormat getNumberFormat(String columnId) {
    DecimalFormat format = _numberFormatMap.get(columnId);
    if (format == null) {
        DecimalFormatSymbols numberSymbols = new DecimalFormatSymbols();
        numberSymbols.setGroupingSeparator(dataSetDef.getNumberGroupSeparator(columnId));
        numberSymbols.setDecimalSeparator(dataSetDef.getNumberDecimalSeparator(columnId));
        format = new DecimalFormat(dataSetDef.getPattern(columnId), numberSymbols);
        _numberFormatMap.put(columnId, format);
    }/*from  w  w  w  .j  a v a2  s . c  om*/
    return format;
}

From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java

/**
 * Parses a Number into a SWIFT string number ####,## with truncated zero decimals and mandatory decimal separator.
 * <ul>// w w w.  ja va2 s .c  o m
 *    <li>Example: 1234.00 -> 1234,</li>
 *    <li>Example: 1234 -> 1234,</li>
 *    <li>Example: 1234.56 -> 1234,56</li>
 * </ul>
 * @param number to parse
 * @return Number of the parsed amount or <code>null</code> if the number is null
 */
public static String getNumber(final Number number) {
    if (number != null) {
        final DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        final DecimalFormat df = new DecimalFormat("0.##########", symbols);
        df.setParseBigDecimal(true);
        df.setDecimalSeparatorAlwaysShown(true);
        final String formatted = df.format(number);
        final String result = StringUtils.replaceChars(formatted, '.', ',');
        return result;
    }
    return null;
}

From source file:org.dashbuilder.dataprovider.csv.CSVParser.java

protected DecimalFormat getNumberFormat(String columnId) {
    DecimalFormat format = _numberFormatMap.get(columnId);
    if (format == null) {
        DecimalFormatSymbols numberSymbols = new DecimalFormatSymbols();
        numberSymbols.setGroupingSeparator(dataSetDef.getNumberGroupSeparator(columnId));
        numberSymbols.setDecimalSeparator(dataSetDef.getNumberDecimalSeparator(columnId));
        format = new DecimalFormat(dataSetDef.getNumberPattern(columnId), numberSymbols);
        _numberFormatMap.put(columnId, format);
    }/*w  w w.  ja v  a 2s .c  o  m*/
    return format;
}

From source file:ch.entwine.weblounge.common.impl.content.image.ImageContentImpl.java

/**
 * {@inheritDoc}/*w  w w  . j  a  v a2  s .co m*/
 * 
 * @see ch.entwine.weblounge.common.impl.content.ResourceContentImpl#extendXml(java.lang.StringBuffer)
 */
@Override
protected StringBuffer extendXml(StringBuffer xml) {
    xml = super.extendXml(xml);
    if (width <= 0)
        throw new IllegalArgumentException("Image must be wider than 0 pixels");
    if (height <= 0)
        throw new IllegalArgumentException("Image must be taller than 0 pixels");

    DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
    formatSymbols.setDecimalSeparator('.');
    NumberFormat nf = new DecimalFormat("0.000000", formatSymbols);

    xml.append("<width>").append(width).append("</width>");
    xml.append("<height>").append(height).append("</height>");

    if (dateTaken != null) {
        xml.append("<datetaken>").append(WebloungeDateFormat.formatStatic(dateTaken)).append("</datetaken>");
    }
    if (!StringUtils.isBlank(location)) {
        xml.append("<location><![CDATA[").append(location).append("]]></location>");
    }
    if (gpsLat != -1 && gpsLong != -1) {
        xml.append("<gps lat=\"").append(nf.format(gpsLat)).append("\" lng=\"").append(nf.format(gpsLong))
                .append("\" />");
    }
    if (filmspeed != -1) {
        xml.append("<filmspeed>").append(filmspeed).append("</filmspeed>");
    }
    if (fnumber != -1) {
        xml.append("<fnumber>").append(fnumber).append("</fnumber>");
    }
    if (focalWidth != -1) {
        xml.append("<focalwidth>").append(focalWidth).append("</focalwidth>");
    }
    if (exposureTime != -1) {
        xml.append("<exposuretime>").append(exposureTime).append("</exposuretime>");
    }
    return xml;
}

From source file:com.coinblesk.client.utils.UIUtils.java

public static String coinToAmount(Context context, Coin coin) {
    // transform a given coin value to the "amount string".
    BigDecimal coinAmount = new BigDecimal(coin.getValue());
    BigDecimal div = new BigDecimal(Coin.COIN.getValue());

    if (SharedPrefUtils.isBitcoinScaleBTC(context)) {
        div = new BigDecimal(Coin.COIN.getValue());
    } else if (SharedPrefUtils.isBitcoinScaleMilliBTC(context)) {
        div = new BigDecimal(Coin.MILLICOIN.getValue());
    } else if (SharedPrefUtils.isBitcoinScaleMicroBTC(context)) {
        div = new BigDecimal(Coin.MICROCOIN.getValue());
    }//from w  w w. j a v a  2s .c  o m

    DecimalFormat df = new DecimalFormat("#.####");
    df.setRoundingMode(RoundingMode.DOWN);
    df.setMaximumFractionDigits(4);
    DecimalFormatSymbols decFormat = new DecimalFormatSymbols();
    decFormat.setDecimalSeparator('.');
    df.setDecimalFormatSymbols(decFormat);
    String amount = df.format(coinAmount.divide(div));
    return amount;
}

From source file:org.openspaces.grid.gsm.autoscaling.AutoScalingSlaUtils.java

private static DecimalFormat initDecimalFormat() {
    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
    decimalFormatSymbols.setDecimalSeparator('.');
    decimalFormatSymbols.setGroupingSeparator(',');
    return new DecimalFormat("#,##0.##", decimalFormatSymbols);
}

From source file:org.totschnig.myexpenses.util.Utils.java

/**
 * @param currency/*from   www  . ja v  a  2s .co m*/
 * @param separator
 * @return a Decimalformat with the number of fraction digits appropriate for
 *         currency, and with the given separator, but without the currency
 *         symbol appropriate for CSV and QIF export
 */
public static DecimalFormat getDecimalFormat(Currency currency, char separator) {
    DecimalFormat nf = new DecimalFormat();
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator(separator);
    nf.setDecimalFormatSymbols(symbols);
    int fractionDigits = currency.getDefaultFractionDigits();
    if (fractionDigits != -1) {
        nf.setMinimumFractionDigits(fractionDigits);
        nf.setMaximumFractionDigits(fractionDigits);
    } else {
        nf.setMaximumFractionDigits(Money.DEFAULTFRACTIONDIGITS);
    }
    nf.setGroupingUsed(false);
    return nf;
}

From source file:LPGoogleFunctions.LPGoogleFunctions.java

/**
 * The Google Directions API is a service that calculates directions between locations using an HTTP request.
 * You can search for directions for several modes of transportation, include transit, driving, walking or cycling. 
 * Directions may specify origins, destinations and waypoints either as text strings (e.g. "Chicago, IL" or "Darwin, NT, Australia") or as latitude/longitude coordinates.
 * The Directions API can return multi-part directions using a series of waypoints.
 * @param origin - The latitude/longitude value from which you wish to calculate directions.
 * @param destination - The latitude/longitude value from which you wish to calculate directions.
 * @param travelMode - Specifies the mode of transport to use when calculating directions. If you set the mode to "transit" you must also specify either a departure time or an arrival time.
 * @param avoid - Indicates that the calculated route(s) should avoid the indicated features.
 * @param unit - Specifies the unit system to use when displaying results.
 * @param alternatives - If set to true, specifies that the Directions service may provide more than one route alternative in the response. Note that providing route alternatives may increase the response time from the server.
 * @param departureTimeSeconds - Specifies the desired time of departure.
 * @param arrivalTimeSeconds - Specifies the desired time of arrival.
 * @param waypoints - Specifies an array of waypoints. Waypoints alter a route by routing it through the specified location(s). A waypoint is specified as either a latitude/longitude coordinate or as an address which will be geocoded.
 * @param responseHandler//from   w w  w.  ja va2s. c  om
 * @Override public void willLoadDirections()
 * @Override public void didLoadDirections(LPDirections directions)
 * @Override public void errorLoadingDirections(LPGoogleStatus status)
 */

public void loadDirectionsForOrigin(LPLocation origin, LPLocation destination,
        final LPGoogleDirectionsTravelMode travelMode, LPGoogleDirectionsAvoid avoid,
        LPGoogleDirectionsUnit unit, boolean alternatives, long departureTimeSeconds, long arrivalTimeSeconds,
        ArrayList<LPWaypoint> waypoints, final DirectionsListener responseHandler) {
    if (responseHandler != null)
        responseHandler.willLoadDirections();

    RequestParams parameters = new RequestParams();

    DecimalFormatSymbols separator = new DecimalFormatSymbols();
    separator.setDecimalSeparator('.');
    DecimalFormat coordinateDecimalFormat = new DecimalFormat("##.######");
    coordinateDecimalFormat.setDecimalFormatSymbols(separator);

    if (origin != null)
        parameters.put("origin", String.format("%s,%s", coordinateDecimalFormat.format(origin.latitude),
                coordinateDecimalFormat.format(origin.longitude)));
    if (destination != null)
        parameters.put("destination",
                String.format("%s,%s", coordinateDecimalFormat.format(destination.latitude),
                        coordinateDecimalFormat.format(destination.longitude)));
    parameters.put("sensor", this.sensor ? "true" : "false");
    parameters.put("language", this.languageCode);

    if (travelMode != null) {
        parameters.put("mode", LPStep.getDirectionsTravelMode(travelMode));
    }

    if (avoid != null) {
        parameters.put("avoid", LPDirections.getDirectionsAvoid(avoid));
    }

    if (unit != null) {
        parameters.put("units", LPDirections.getDirectionsUnit(unit));
    }

    parameters.put("alternatives", alternatives ? "true" : "false");

    if (departureTimeSeconds > 0) {
        parameters.put("departure_time", String.valueOf(departureTimeSeconds));
    }

    if (arrivalTimeSeconds > 0) {
        parameters.put("arrival_time", String.valueOf(arrivalTimeSeconds));
    }

    if (waypoints != null) {
        StringBuilder waypointsString = new StringBuilder();

        for (int i = 0; i < waypoints.size(); i++) {
            LPWaypoint waypoint = waypoints.get(i);

            waypointsString
                    .append(String.format("%s,%s|", coordinateDecimalFormat.format(waypoint.location.latitude),
                            coordinateDecimalFormat.format(waypoint.location.longitude)));
        }

        parameters.put("waypoints", waypointsString.toString());
    }

    this.client.get(googleAPIDirectionsURL, parameters, new AsyncHttpResponseHandler() {
        @Override
        public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
            if (responseHandler != null)
                responseHandler.errorLoadingDirections(LPGoogleStatus.LPGoogleStatusUnknownError);
        }

        @Override
        public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
            try {
                String response = new String(arg2, "UTF-8");

                JSONObject object = new JSONObject(response);

                LPDirections directions = new LPDirections(object);
                directions.requestTravelMode = travelMode;

                LPGoogleStatus status = LPGoogleFunctions.getGoogleStatusFromString(directions.statusCode);

                if (status == LPGoogleStatus.LPGoogleStatusOK) {
                    if (responseHandler != null)
                        responseHandler.didLoadDirections(directions);
                } else {
                    if (responseHandler != null)
                        responseHandler.errorLoadingDirections(status);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();

                if (responseHandler != null)
                    responseHandler.errorLoadingDirections(LPGoogleStatus.LPGoogleStatusUnknownError);
            } catch (JSONException e) {
                e.printStackTrace();

                if (responseHandler != null)
                    responseHandler.errorLoadingDirections(LPGoogleStatus.LPGoogleStatusUnknownError);
            }
        }
    });
}