当前位置: 首页>>代码示例>>Java>>正文


Java DecimalFormat.parse方法代码示例

本文整理汇总了Java中java.text.DecimalFormat.parse方法的典型用法代码示例。如果您正苦于以下问题:Java DecimalFormat.parse方法的具体用法?Java DecimalFormat.parse怎么用?Java DecimalFormat.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.text.DecimalFormat的用法示例。


在下文中一共展示了DecimalFormat.parse方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: build

import java.text.DecimalFormat; //导入方法依赖的package包/类
private void build() {
    try {

        if (typeQualifier != null) {
            buildQualifiedNumber();
            return;
        }

        Number value;
        if (number.contains(".")) {
            DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
            decimalFormat.setParseBigDecimal(true);

            int decSymbolIndex = number.lastIndexOf(".");
            if (decSymbolIndex > -1) {
                int precision = number.substring(decSymbolIndex, number.length() - 1).length();
                decimalFormat.setMaximumFractionDigits(precision);
            }

            value = decimalFormat.parse(number);
        } else {
            value = NumberFormat.getInstance(locale).parse(number);
        }

        set(value);
    } catch (ParseException e) {
        throw new RuntimeException(String.format("Invalid number '%s'", number));
    }
}
 
开发者ID:edmocosta,项目名称:queryfy,代码行数:30,代码来源:NumberVar.java

示例2: convertToBigDecimal

import java.text.DecimalFormat; //导入方法依赖的package包/类
private static BigDecimal convertToBigDecimal(Object o) {
    DecimalFormat df = new DecimalFormat();
    df.setParseBigDecimal(true);
    try {
        return (BigDecimal) df.parse(o.toString());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
开发者ID:intuit,项目名称:karate,代码行数:11,代码来源:Script.java

示例3: parse

import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
 * Regardless of the default locale, comma ('.') is used as decimal separator
 *
 * @param source
 * @return
 * @throws ParseException
 */
public BigDecimal parse(String source) throws ParseException {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator('.');
    DecimalFormat format = new DecimalFormat("#.#", symbols);
    format.setParseBigDecimal(true);
    return (BigDecimal) format.parse(source);
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:15,代码来源:LocaleSafeDecimalFormat.java

示例4: parse

import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
 * Convert the specified locale-sensitive input object into an output
 * object of the specified type.
 *
 * @param value The input object to be converted
 * @param pattern The pattern is used for the convertion
 * @return The converted value
 *
 * @throws org.apache.commons.beanutils.ConversionException if conversion
 * cannot be performed successfully
 * @throws ParseException if an error occurs parsing a String to a Number
 */
@Override
protected Object parse(final Object value, final String pattern) throws ParseException {

    if (value instanceof Number) {
        return value;
    }

    // Note that despite the ambiguous "getInstance" name, and despite the
    // fact that objects returned from this method have the same toString
    // representation, each call to getInstance actually returns a new
    // object.
    final DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(locale);

    // if some constructors default pattern to null, it makes only sense
    // to handle null pattern gracefully
    if (pattern != null) {
        if (locPattern) {
            formatter.applyLocalizedPattern(pattern);
        } else {
            formatter.applyPattern(pattern);
        }
    } else {
        log.debug("No pattern provided, using default.");
    }

    return formatter.parse((String) value);
}
 
开发者ID:yippeesoft,项目名称:NotifyTools,代码行数:40,代码来源:DecimalLocaleConverter.java

示例5: isDecimal

import java.text.DecimalFormat; //导入方法依赖的package包/类
public static boolean isDecimal(String input) {
  try {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setGroupingSeparator(',');
    symbols.setDecimalSeparator('.');
    String pattern = "#,##0.0#";
    DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
    BigDecimal bigDecimal = (BigDecimal) decimalFormat.parse(input);
    return true;
  } catch (Exception ex) {
    return false;
  }
}
 
开发者ID:HPI-Information-Systems,项目名称:metanome-algorithms,代码行数:14,代码来源:DataTypes.java

示例6: getParsedDuration

import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
 * Parses the given value considering the current locale and returns the
 * number representation of the string. The representation is based on the
 * {@link #DURATION_FORMAT}.
 * 
 * @param valueToCheck
 *            The string to be parsed. Must not be <code>null</code>.
 * @return The number representation of the parameter.
 * @throws ParseException
 */
public static Number getParsedDuration(FacesContext facesContext,
        String valueToCheck) {
    Locale locale = LocaleHandler.getLocaleFromString(BaseBean
            .getUserFromSession(facesContext).getLocale());
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
    DecimalFormat df = new DecimalFormat(DURATION_FORMAT, dfs);
    df.setGroupingUsed(true);
    try {
        return df.parse(valueToCheck);
    } catch (ParseException e) {
        return null;
    }
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:24,代码来源:DurationValidation.java

示例7: getParsedDuration

import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
 * Taken from org.oscm.ui.common.DurationValidation
 * 
 * @param valueToCheck
 * @return
 */
private Number getParsedDuration(String valueToCheck) {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.getDefault());
    DecimalFormat df = new DecimalFormat(DURATION_FORMAT, dfs);
    df.setGroupingUsed(true);
    try {
        return df.parse(valueToCheck);
    } catch (ParseException e) {
        return null;
    }
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:17,代码来源:DurationParameterValidator.java

示例8: getDecimalNumber

import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
 * Returns parsed number.
 * @param number String number representation.
 * @return Parsed number.
 * @throws ParseException Unable to parse given string.
 */
private Number getDecimalNumber(String number) throws ParseException {
	DecimalFormat format = new DecimalFormat();
	DecimalFormatSymbols custom=new DecimalFormatSymbols();
	custom.setDecimalSeparator('.');
	format.setDecimalFormatSymbols(custom);
	return format.parse(number);
}
 
开发者ID:fgulan,项目名称:java-course,代码行数:14,代码来源:ValueWrapper.java

示例9: amount

import java.text.DecimalFormat; //导入方法依赖的package包/类
@Test
public void amount() throws ParseException {
    Money money = createObjectFromHtml(Money.class);
    DecimalFormat format = new DecimalFormat("0,000.00");
    format.setParseBigDecimal(true);
    BigDecimal expected = (BigDecimal) format.parse("50,000.00");
    assertEquals(money.amount, expected);
}
 
开发者ID:DroidsOnRoids,项目名称:jspoon,代码行数:9,代码来源:BigDecimalFormatTest.java

示例10: createDecimalFormatter

import java.text.DecimalFormat; //导入方法依赖的package包/类
private static TextFormatter<String> createDecimalFormatter() {
	DecimalFormat format = new DecimalFormat("#.0");
	format.setNegativePrefix("-");
	return new TextFormatter<>(c -> {
		if (c.getControlNewText().isEmpty()) return c;
		ParsePosition pos = new ParsePosition(0);
		Number result = format.parse(c.getControlNewText(), pos);
		if (result == null || pos.getIndex() < c.getControlNewText().length()) {
			return null;
		} else return c;
	});
}
 
开发者ID:tom91136,项目名称:GestureFX,代码行数:13,代码来源:LenaSample.java

示例11: ofDouble

import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
 * Returns a newly created Parser for Double
 * @param pattern   the decimal format pattern
 * @param multiplier    the multiplier to apply
 * @return  newly created Parser
 */
public static Parser<Double> ofDouble(String pattern, int multiplier) {
    final DecimalFormat decimalFormat = createDecimalFormat(pattern, multiplier);
    return new ParserOfDouble(defaultNullCheck, value -> {
        try {
            return decimalFormat.parse(value);
        } catch (Exception ex) {
            throw new FormatException("Failed to parse value into double: " + value, ex);
        }
    });
}
 
开发者ID:zavtech,项目名称:morpheus-core,代码行数:17,代码来源:Parser.java

示例12: getDecimalNumber

import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
 * Returns parsed number.
 * 
 * @param number
 *            String number representation.
 * @return Parsed number.
 * @throws ParseException
 *             Unable to parse given string.
 */
private Number getDecimalNumber(String number) throws ParseException {
    DecimalFormat format = new DecimalFormat();
    DecimalFormatSymbols custom = new DecimalFormatSymbols();
    custom.setDecimalSeparator('.');
    format.setDecimalFormatSymbols(custom);
    return format.parse(number);
}
 
开发者ID:fgulan,项目名称:java-course,代码行数:17,代码来源:ValueWrapper.java


注:本文中的java.text.DecimalFormat.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。