當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。