Example usage for java.text DecimalFormatSymbols setDecimalSeparator

List of usage examples for java.text DecimalFormatSymbols setDecimalSeparator

Introduction

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

Prototype

public void setDecimalSeparator(char decimalSeparator) 

Source Link

Document

Sets the character used for decimal sign.

Usage

From source file:org.openossad.util.core.util.StringUtil.java

public static double str2num(String pattern, String decimal, String grouping, String currency, String value)
        throws OpenDESIGNERValueException {
    // 0 : pattern
    // 1 : Decimal separator
    // 2 : Grouping separator
    // 3 : Currency symbol

    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat) nf;
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();

    if (!Const.isEmpty(pattern))
        df.applyPattern(pattern);/* ww w  .ja  va2s  .  c  om*/
    if (!Const.isEmpty(decimal))
        dfs.setDecimalSeparator(decimal.charAt(0));
    if (!Const.isEmpty(grouping))
        dfs.setGroupingSeparator(grouping.charAt(0));
    if (!Const.isEmpty(currency))
        dfs.setCurrencySymbol(currency);
    try {
        df.setDecimalFormatSymbols(dfs);
        return df.parse(value).doubleValue();
    } catch (Exception e) {
        String message = "Couldn't convert string to number " + e.toString();
        if (!isEmpty(pattern))
            message += " pattern=" + pattern;
        if (!isEmpty(decimal))
            message += " decimal=" + decimal;
        if (!isEmpty(grouping))
            message += " grouping=" + grouping.charAt(0);
        if (!isEmpty(currency))
            message += " currency=" + currency;
        throw new OpenDESIGNERValueException(message);
    }
}

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  .  j av a 2 s  . c  om*/
 *    <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: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 w w  w . j a v a  2s  .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.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 www .ja v a2 s . c om

    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.pentaho.di.core.util.StringUtil.java

public static double str2num(String pattern, String decimal, String grouping, String currency, String value)
        throws KettleValueException {
    // 0 : pattern
    // 1 : Decimal separator
    // 2 : Grouping separator
    // 3 : Currency symbol

    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat) nf;
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();

    if (!Const.isEmpty(pattern)) {
        df.applyPattern(pattern);//from w ww. j  a  v a2 s  .c o m
    }
    if (!Const.isEmpty(decimal)) {
        dfs.setDecimalSeparator(decimal.charAt(0));
    }
    if (!Const.isEmpty(grouping)) {
        dfs.setGroupingSeparator(grouping.charAt(0));
    }
    if (!Const.isEmpty(currency)) {
        dfs.setCurrencySymbol(currency);
    }
    try {
        df.setDecimalFormatSymbols(dfs);
        return df.parse(value).doubleValue();
    } catch (Exception e) {
        String message = "Couldn't convert string to number " + e.toString();
        if (!isEmpty(pattern)) {
            message += " pattern=" + pattern;
        }
        if (!isEmpty(decimal)) {
            message += " decimal=" + decimal;
        }
        if (!isEmpty(grouping)) {
            message += " grouping=" + grouping.charAt(0);
        }
        if (!isEmpty(currency)) {
            message += " currency=" + currency;
        }
        throw new KettleValueException(message);
    }
}

From source file:tools.help.java

/**
 * vlozi hodnotu to text fildu a upravi jej pocet des miest
 *
 * @param Y text field vstup//from  w ww.  ja  v  a2s.  c o m
 * @param input value ktoru chem zobrazit v textfielde
 * @param pocetDesMiest pocet desatinnich miest na ktore bude zaukruhlovat
 * cislo
 */
public static void DisplayDouble(javax.swing.JTextField Y, double input, int pocetDesMiest) {

    String symbol = "###.";
    for (int cl0 = 0; cl0 < pocetDesMiest; cl0++) {
        symbol = symbol + "#";
    }

    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols();
    otherSymbols.setDecimalSeparator('.');
    DecimalFormat df = new DecimalFormat(symbol, otherSymbols); // definovany po?et desatinnych miest
    Y.setText(df.format(input));

}

From source file:com.aw.core.format.NumberFormatter.java

public NumberFormatter(String formatPattern) {
    DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols();
    unusualSymbols.setDecimalSeparator('.');
    unusualSymbols.setGroupingSeparator(',');
    this.format = new DecimalFormat(formatPattern, unusualSymbols);
}

From source file:com.receipts.services.ExportService.java

public InputStream createExportFile(Store store) {
    InputStream is = null;/*from   www.  j  a va 2s.  co  m*/

    String storeId = store.getStoreId();
    String storeIdStr = StringUtils.leftPad(storeId, 3, "0");

    Date storeDate = store.getStoreDate();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    String storeDateStr = sdf.format(storeDate);

    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');

    StringBuilder sb = new StringBuilder();
    for (Receipt receipt : store.getReceipts()) {
        if (receipt.isComplete()) {
            String receiptId = receipt.getId().toString();
            String receiptTime = receipt.getReceiptTime();

            for (Product product : receipt.getProducts()) {
                // Store Id: 3 chars
                storeIdStr = StringUtils.substring(storeIdStr, 0, 3);
                sb.append(storeIdStr);

                // Product Name: 32 chars
                String productName = StringUtils.substring(product.getProductName(), 0, 32);
                String productNameStr = StringUtils.rightPad(productName, 32);
                sb.append(productNameStr);

                String productPrice = new DecimalFormat("0000.00", dfs).format(product.getProductPrice());
                String productPriceStr = StringUtils.leftPad(productPrice, 7, "0");
                sb.append(productPriceStr);

                // Receipt Id: 10 chars
                String receiptIdStr = StringUtils.leftPad(StringUtils.substring(receiptId, 0, 10), 10);
                sb.append(receiptIdStr);

                sb.append(storeDateStr);

                sb.append(receiptTime);

                String productQuantity = new DecimalFormat("#0.000", dfs).format(product.getProductQuantity());
                String productQuantityStr = StringUtils.leftPad(productQuantity, 6, "0");
                sb.append(productQuantityStr);

                sb.append(System.getProperty("line.separator"));
            }
        }
    }

    is = IOUtils.toInputStream(sb.toString());

    return is;
}

From source file:com.aw.core.format.NullDecimalFormat.java

/**
 * Constructor/*from   w  w  w  .  j  a  va  2s. c  o m*/
 *
 * @param pattern
 */
public NullDecimalFormat(String pattern) {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator('.');
    symbols.setGroupingSeparator(',');
    decimalFormat = new DecimalFormat(pattern, symbols);
}

From source file:org.gbif.ipt.struts2.converter.CoordinateFormatConverter.java

@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
    // The null value is needed to validate in EmlValidator.java class
    if (values[0].length() == 0) {
        return null;
    }//from   w w  w . ja  v  a  2 s  . c o m
    // The full name of the property which call the method contained in the Map context
    Object coordObject = context.get(ANGLE);
    // The latitude is validating in a range of doubles
    // validate coordinates in case the action context doesn't work properly.
    if (coordObject == null) {
        throw new TypeConversionException("Invalid decimal number: " + values[0]);
    } else {
        String coordinate = context.get(ANGLE).toString();
        // Assign the values of the range depending the property who calls the method.
        Range<Double> range;
        if (coordinate.equals(CoordinateUtils.LATITUDE)) {
            // The range of the latitude coordinate. (-90,90)
            range = Range.between(CoordinateUtils.MIN_LATITUDE, CoordinateUtils.MAX_LATITUDE);
        } else {
            // The range of the longitude coordinate. (-180,180)
            range = Range.between(CoordinateUtils.MIN_LONGITUDE, CoordinateUtils.MAX_LONGITUDE);
        }

        Double number;
        try {
            // Converts String to double if fails throws a NumberFormatException.
            // If the String contains a comma, a character, it throws the exception.
            number = Double.parseDouble(values[0]);
            // If the value is in the range, returns the double.
            if (range.contains(number)) {
                return number;
            } else {
                throw new TypeConversionException("Invalid decimal number: " + values[0]);
            }
        } catch (NumberFormatException e) {
            // Creating a pattern which will convert the comma to period
            // It will return a ParseException if the format was wrong.
            DecimalFormatSymbols symbols = new DecimalFormatSymbols();
            symbols.setDecimalSeparator(ALTERNATIVE_DECIMAL_SEPARATOR);
            DecimalFormat decimal = new DecimalFormat(DECIMAL_PATTERN, symbols);
            try {
                number = decimal.parse(values[0]).doubleValue();
                if (range.contains(number)) {
                    return number;
                } else {
                    throw new TypeConversionException("Invalid decimal number: " + values[0]);
                }
            } catch (ParseException e1) {
                throw new TypeConversionException("Invalid decimal number: " + values[0]);
            }
        }
    }
}