本文整理汇总了Java中java.text.NumberFormat.parse方法的典型用法代码示例。如果您正苦于以下问题:Java NumberFormat.parse方法的具体用法?Java NumberFormat.parse怎么用?Java NumberFormat.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.NumberFormat
的用法示例。
在下文中一共展示了NumberFormat.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toNumber
import java.text.NumberFormat; //导入方法依赖的package包/类
/**
* Converts null, dates, numbers and strings to a Number.
*
* @param in
* @param format
* @return
*/
public static synchronized final Number toNumber(Object in, NumberFormat format) {
if (in == null)
return 0.0;
else if (in instanceof Number)
return (Number) in;
else if (in instanceof Date)
return ((Date) in).getTime();
else if (in instanceof Calendar)
return ((Calendar) in).getTimeInMillis();
else if (in instanceof String)
try {
return ((String) in).isEmpty()
? 0.0
: format.parse((String) in);
} catch (Exception e) {
throw new RuntimeException(e);
}
else
throw new RuntimeException("Can't convert " + in.getClass() + " to " + Number.class);
}
示例2: parse
import java.text.NumberFormat; //导入方法依赖的package包/类
/**
* Convert a String into a <code>Number</code> object.
* @param sourceType the source type of the conversion
* @param targetType The type to convert the value to
* @param value The String date value.
* @param format The NumberFormat to parse the String value.
*
* @return The converted Number object.
* @throws ConversionException if the String cannot be converted.
*/
private Number parse(final Class<?> sourceType, final Class<?> targetType, final String value, final NumberFormat format) {
final ParsePosition pos = new ParsePosition(0);
final Number parsedNumber = format.parse(value, pos);
if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) {
String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'";
if (format instanceof DecimalFormat) {
msg += " using pattern '" + ((DecimalFormat)format).toPattern() + "'";
}
if (locale != null) {
msg += " for locale=[" + locale + "]";
}
if (log().isDebugEnabled()) {
log().debug(" " + msg);
}
throw new ConversionException(msg);
}
return parsedNumber;
}
示例3: parse
import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
public Number parse(String text, Locale locale) throws ParseException {
NumberFormat format = getNumberFormat(locale);
ParsePosition position = new ParsePosition(0);
Number number = format.parse(text, position);
if (position.getErrorIndex() != -1) {
throw new ParseException(text, position.getIndex());
}
if (!this.lenient) {
if (text.length() != position.getIndex()) {
// indicates a part of the string that was not parsed
throw new ParseException(text, position.getIndex());
}
}
return number;
}
示例4: getLocalisedFloat
import java.text.NumberFormat; //导入方法依赖的package包/类
/**
* Convert a string back into a Float. Assumes string was formatted using formatLocalisedNumber originally. Should
* ensure that it is using the same locale/number format as when it was formatted. If no locale is suppied, it will
* use the server's locale
*
* Need to strip out any spaces as spaces are valid group separators in some European locales (e.g. Polish) but they
* seem to come back from Firefox as a plain space rather than the special separating space.
*/
public static Float getLocalisedFloat(String inputStr, Locale locale) {
String numberStr = inputStr;
if (numberStr != null) {
numberStr = numberStr.replace(" ", "");
}
if ((numberStr != null) && (numberStr.length() > 0)) {
Locale useLocale = locale != null ? locale : NumberUtil.getServerLocale();
NumberFormat format = NumberFormat.getInstance(useLocale);
ParsePosition pp = new ParsePosition(0);
Number num = format.parse(numberStr, pp);
if ((num != null) && (pp.getIndex() == numberStr.length())) {
return num.floatValue();
}
}
throw new NumberFormatException("Unable to convert number " + numberStr + "to float using locale "
+ locale.getCountry() + " " + locale.getLanguage());
}
示例5: getLocalisedDouble
import java.text.NumberFormat; //导入方法依赖的package包/类
/**
* Convert a string back into a Float. Assumes string was formatted using formatLocalisedNumber originally. Should
* ensure that it is using the same locale/number format as when it was formatted. If no locale is suppied, it will
* use the server's locale.
*
* Need to strip out any spaces as spaces are valid group separators in some European locales (e.g. Polish) but they
* seem to come back from Firefox as a plain space rather than the special separating space.
*/
public static Double getLocalisedDouble(String inputStr, Locale locale) {
String numberStr = inputStr;
if (numberStr != null) {
numberStr = numberStr.replace(" ", "");
}
if ((numberStr != null) && (numberStr.length() > 0)) {
Locale useLocale = locale != null ? locale : NumberUtil.getServerLocale();
NumberFormat format = NumberFormat.getInstance(useLocale);
ParsePosition pp = new ParsePosition(0);
Number num = format.parse(numberStr, pp);
if ((num != null) && (pp.getIndex() == numberStr.length())) {
return num.doubleValue();
}
}
throw new NumberFormatException("Unable to convert number " + numberStr + "to double using locale "
+ locale.getCountry() + " " + locale.getLanguage());
}
示例6: parseOnPattern
import java.text.NumberFormat; //导入方法依赖的package包/类
private static void parseOnPattern(NumberFormat nf, String pattern,
String parseString, Number expected) {
if (nf instanceof DecimalFormat) {
((DecimalFormat) nf).applyPattern(pattern);
}
try {
Number output = nf.parse(parseString);
if (expected.doubleValue() != output.doubleValue()) {
throw new RuntimeException("[FAILED: Unable to parse the number"
+ " based on the pattern: '" + pattern + "', Expected : '"
+ expected + "', Found: '" + output + "']");
}
} catch (ParseException ex) {
throw new RuntimeException("[FAILED: Unable to parse the pattern:"
+ " '" + pattern + "']", ex);
}
}
示例7: filterBenchmark
import java.text.NumberFormat; //导入方法依赖的package包/类
protected Double filterBenchmark(final List<String> output, final String benchmarkName) throws Exception {
Double currentScore = 0.;
for (final String s : output) {
//if (s.trim().startsWith(benchmarkName)) {
if (s.trim().startsWith("Score")) {
final String[] split = s.split(":");
if (split.length != 2) {
for (final String outString : output) {
System.out.println("outString (score format)"+outString);
}
throw new IllegalArgumentException("Invalid benchmark output format");
}
final NumberFormat nf = NumberFormat.getInstance();
final Number _newCurrentScore = nf.parse(split[1].trim());
final Double newCurrentScore = _newCurrentScore.doubleValue();
if (currentScore < newCurrentScore) {
currentScore = newCurrentScore;
}
}
}
// System.out.println("filterBenchmark current score:"+currentScore);
return currentScore;
}
示例8: parseReading
import java.text.NumberFormat; //导入方法依赖的package包/类
/**
* A convenient method for parsing reading value based on user's locale
*
* @param reading reading number String
* @return reading Number
*/
@Nullable
public static Number parseReading(String reading) {
if (reading == null)
return null;
NumberFormat numberFormat = NumberFormat.getInstance();
try {
return numberFormat.parse(reading);
} catch (ParseException e) {
return null;
}
}
示例9: truncateDouble
import java.text.NumberFormat; //导入方法依赖的package包/类
public static double truncateDouble(double d, int numDecimal) {
double td = 0;
NumberFormat format = NumberFormat.getInstance();
// %.<string>f, d <-- this is wrong on so many ways
String s = String.format("%." + Integer.toString(numDecimal) + "f", d);
try {
Number number = format.parse(s);
td = number.doubleValue();
} catch (ParseException e) {
Log.e(TAG, mTAG + "parsing exception", e);
}
return td;
}
示例10: parseValue
import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
protected Double parseValue(String arg, Locale locale) throws IllegalOptionValueException {
try {
NumberFormat format = NumberFormat.getNumberInstance(locale);
Number num = (Number) format.parse(arg);
return new Double(num.doubleValue());
} catch (ParseException e) {
throw new IllegalOptionValueException(this, arg);
}
}
示例11: parseFee
import java.text.NumberFormat; //导入方法依赖的package包/类
private static float parseFee(String managementFee) throws ComparisonException, ParseException {
float fee;
if (managementFee.contains(",")){
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse(managementFee);
fee = number.floatValue()/100;
}
else {
fee = Float.parseFloat(managementFee)/100;
}
if (fee > 0.02 || fee < 0) throw new FeeSizeException("Fee size does not match Estonian standards");
return fee;
}
示例12: parseValue
import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
protected Double parseValue(String arg, Locale locale)
throws IllegalOptionValueException {
try {
NumberFormat format = NumberFormat.getNumberInstance(locale);
Number num = (Number) format.parse(arg);
return new Double(num.doubleValue());
} catch (ParseException e) {
throw new IllegalOptionValueException(this, arg);
}
}
示例13: transformUnit
import java.text.NumberFormat; //导入方法依赖的package包/类
public static Double transformUnit(String value, Unit unit) throws ParseException {
value = value.replaceAll("[^0-9\\,\\.\\-Ee\\+]", "");
NumberFormat format = NumberFormat.getInstance(Locale.US);
Number number = format.parse(value);
Double valueBeforeTransformation = number.doubleValue();
return valueBeforeTransformation * unit.getFactor();
}
示例14: postProcessNode
import java.text.NumberFormat; //导入方法依赖的package包/类
@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
if (node instanceof FieldQueryNode
&& !(node.getParent() instanceof RangeQueryNode)) {
QueryConfigHandler config = getQueryConfigHandler();
if (config != null) {
FieldQueryNode fieldNode = (FieldQueryNode) node;
FieldConfig fieldConfig = config.getFieldConfig(fieldNode
.getFieldAsString());
if (fieldConfig != null) {
NumericConfig numericConfig = fieldConfig
.get(ConfigurationKeys.NUMERIC_CONFIG);
if (numericConfig != null) {
NumberFormat numberFormat = numericConfig.getNumberFormat();
String text = fieldNode.getTextAsString();
Number number = null;
if (text.length() > 0) {
try {
number = numberFormat.parse(text);
} catch (ParseException e) {
throw new QueryNodeParseException(new MessageImpl(
QueryParserMessages.COULD_NOT_PARSE_NUMBER, fieldNode
.getTextAsString(), numberFormat.getClass()
.getCanonicalName()), e);
}
switch (numericConfig.getType()) {
case LONG:
number = number.longValue();
break;
case INT:
number = number.intValue();
break;
case DOUBLE:
number = number.doubleValue();
break;
case FLOAT:
number = number.floatValue();
}
} else {
throw new QueryNodeParseException(new MessageImpl(
QueryParserMessages.NUMERIC_CANNOT_BE_EMPTY, fieldNode.getFieldAsString()));
}
NumericQueryNode lowerNode = new NumericQueryNode(fieldNode
.getField(), number, numberFormat);
NumericQueryNode upperNode = new NumericQueryNode(fieldNode
.getField(), number, numberFormat);
return new NumericRangeQueryNode(lowerNode, upperNode, true, true,
numericConfig);
}
}
}
}
return node;
}
示例15: getNumberFromString
import java.text.NumberFormat; //导入方法依赖的package包/类
private Number getNumberFromString(String value) throws ParseException {
NumberFormat numberFormat = NumberFormat.getInstance(locale);
return numberFormat.parse(value);
}