本文整理汇总了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));
}
}
示例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;
}
}
示例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);
}
示例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);
}
示例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;
}
}
示例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;
}
}
示例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;
}
}
示例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);
}
示例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);
}
示例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;
});
}
示例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);
}
});
}
示例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);
}