Example usage for java.text DecimalFormat setParseBigDecimal

List of usage examples for java.text DecimalFormat setParseBigDecimal

Introduction

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

Prototype

public void setParseBigDecimal(boolean newValue) 

Source Link

Document

Sets whether the #parse(java.lang.String,java.text.ParsePosition) method returns BigDecimal .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.setParseBigDecimal(true);
    System.out.println(format.format(12345678912345678L));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.setParseBigDecimal(true);
    BigDecimal value = (BigDecimal) format.parse("1,234.56");
    System.out.println(value);/*from  ww w .  j a v a2s  . c  o m*/
}

From source file:org.goko.core.common.GkUtils.java

public static BigDecimal toBigDecimal(String str) throws GkException {
    DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
    df.setParseBigDecimal(true);
    try {//  w ww.  j  ava2s  .  c  o m
        return (BigDecimal) df.parse(str);
    } catch (ParseException e) {
        throw new GkTechnicalException(e);
    }
}

From source file:com.qcadoo.model.api.BigDecimalUtils.java

/**
 * Try parse string into BigDecimal./*from  ww w . jav a  2s.  co m*/
 * 
 * @param maybeStringWithDecimal
 *            String to be parsed as a BigDecimal number
 * @param locale
 *            locale to be used when parse.
 * @return either Exception that occur during parsing or parsed BigDecimal, wrapped within Optional.
 */
public static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal,
        final Locale locale) {
    if (StringUtils.isBlank(maybeStringWithDecimal)) {
        return Either.right(Optional.<BigDecimal>absent());
    }
    try {
        DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale);
        format.setParseBigDecimal(true);
        BigDecimal parsedDecimal = new BigDecimal(format.parse(maybeStringWithDecimal).toString());
        return Either.right(Optional.fromNullable(parsedDecimal));
    } catch (Exception e) {
        return Either.left(e);
    }
}

From source file:com.insightaction.util.DataLoader.java

private static BigDecimal getBigDecimal(String value) {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setGroupingSeparator(',');
    symbols.setDecimalSeparator('.');
    String pattern = "#,##0.0#";
    DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
    decimalFormat.setParseBigDecimal(true);

    BigDecimal bigDecimal = null;
    try {//from   w  w w  .j a  va 2 s.com
        bigDecimal = (BigDecimal) decimalFormat.parse(value);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return bigDecimal;
}

From source file:org.apache.stratos.manager.utils.ApplicationManagementUtil.java

public static java.util.Properties setRegisterServiceProperties(Policy policy, int tenantId, String alias) {

    DecimalFormat df = new DecimalFormat("##.##");
    df.setParseBigDecimal(true);

    java.util.Properties properties = new java.util.Properties();
    List<Property> allProperties = new ArrayList<Property>();
    // min_app_instances
    Property property = new Property();
    property.setName("min_app_instances");
    property.setValue(df.format(policy.getMinAppInstances()));
    allProperties.add(property);//from  w  ww  . j  a v  a 2  s .c o m

    // max_app_instances
    property = new Property();
    property.setName("max_app_instances");
    property.setValue(df.format(policy.getMaxAppInstances()));
    allProperties.add(property);

    // max_requests_per_second
    property = new Property();
    property.setName("max_requests_per_second");
    property.setValue(df.format(policy.getMaxRequestsPerSecond()));
    allProperties.add(property);

    // alarming_upper_rate
    property = new Property();
    property.setName("alarming_upper_rate");
    property.setValue(df.format(policy.getAlarmingUpperRate()));
    allProperties.add(property);

    // alarming_lower_rate
    property = new Property();
    property.setName("alarming_lower_rate");
    property.setValue(df.format(policy.getAlarmingLowerRate()));
    allProperties.add(property);

    // scale_down_factor
    property = new Property();
    property.setName("scale_down_factor");
    property.setValue(df.format(policy.getScaleDownFactor()));
    allProperties.add(property);

    // rounds_to_average
    property = new Property();
    property.setName("rounds_to_average");
    property.setValue(df.format(policy.getRoundsToAverage()));
    allProperties.add(property);

    // tenant id
    property = new Property();
    property.setName("tenant_id");
    property.setValue(String.valueOf(tenantId));
    allProperties.add(property);

    // alias
    property = new Property();
    property.setName("alias");
    property.setValue(String.valueOf(alias));
    allProperties.add(property);

    return addToJavaUtilProperties(allProperties);
}

From source file:de.jlo.talendcomp.json.TypeUtil.java

public static Double convertToDouble(String value) throws Exception {
    if (value == null || value.isEmpty()) {
        return null;
    }//  w  w  w  . j  av a 2 s.c  o m
    DecimalFormat decfrm = getNumberFormat(DEFAULT_LOCALE);
    decfrm.setParseBigDecimal(false);
    return decfrm.parse(value).doubleValue();
}

From source file:de.jlo.talendcomp.json.TypeUtil.java

public static Integer convertToInteger(String value) throws Exception {
    if (value == null || value.isEmpty()) {
        return null;
    }/*w  w  w  .j av  a2s. co m*/
    DecimalFormat decfrm = getNumberFormat(DEFAULT_LOCALE);
    decfrm.setParseBigDecimal(false);
    return decfrm.parse(value).intValue();
}

From source file:de.jlo.talendcomp.json.TypeUtil.java

public static BigDecimal convertToBigDecimal(String value) throws Exception {
    if (value == null || value.isEmpty()) {
        return null;
    }/*from w  ww  .j  av  a 2s  . c o m*/
    try {
        DecimalFormat decfrm = getNumberFormat(DEFAULT_LOCALE);
        decfrm.setParseBigDecimal(true);
        ParsePosition pp = new ParsePosition(0);
        return (BigDecimal) decfrm.parse(value, pp);
    } catch (RuntimeException e) {
        throw new Exception("convertToBigDecimal:" + value + " failed:" + e.getMessage(), e);
    }
}

From source file:Main.java

public static String safeDoubleToCurrency(Double val) {
    BigDecimal value = BigDecimal.valueOf(val);
    DecimalFormat kursIndonesia = (DecimalFormat) DecimalFormat.getCurrencyInstance();
    DecimalFormatSymbols formatRp = new DecimalFormatSymbols();

    formatRp.setCurrencySymbol("");
    formatRp.setMonetaryDecimalSeparator('.');
    formatRp.setGroupingSeparator(',');

    kursIndonesia.setDecimalFormatSymbols(formatRp);
    kursIndonesia.setParseBigDecimal(true);
    if (val < 1)
        kursIndonesia.setMaximumFractionDigits(8);
    else if (val < 10)
        kursIndonesia.setMaximumFractionDigits(6);
    else if (val < 100)
        kursIndonesia.setMaximumFractionDigits(4);

    return kursIndonesia.format(value);
}