Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2004-2008 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.binding.convert.converters;
A converter for common number types such as integers and big decimals. Allows the configuration of an explicit number pattern and locale. Works with a general purpose java.text.DecimalFormat instance returned by calling java.text.NumberFormat.getInstance(java.util.Locale) by default. This instance supports parsing any number type generally and will not perform special type-specific logic such as rounding or truncation. Subclasses may override. Will coerse parsed Numbers to the desired numberClass as necessary. If type-coersion results in an overflow condition; for example, what can occur with a Long being coersed to a Short, an exception will be thrown.

Keith Donald
See also:
 public class FormattedStringToNumber extends StringToObject {
 	private static Log logger = LogFactory.getLog(FormattedStringToNumber.class);
 	private boolean lenient;
 	public FormattedStringToNumber(Class numberClass) {

Sets the factory that returns the java.text.NumberFormat instance that will format numbers handled by this converter.

numberFormatFactory the number format factory
 	public void setNumberFormatFactory(NumberFormatFactory numberFormatFactory) {
 		this. = numberFormatFactory;

If this Converter is "lenient" in parsing number strings. A lenient converter does not require that all characters in the String be parsed successfully. Default is false.

the lenient flag
 	public boolean getLenient() {
 		return ;

Sets if this Converter should parse leniently.

lenient the lenient flag
 	public void setLenient(boolean lenient) {
 		this. = lenient;
 	protected Object toObject(String stringClass targetClassthrows Exception {
 		ParsePosition parsePosition = new ParsePosition(0);
 		Number number = format.parse(stringparsePosition);
 		if (number == null) {
 			// no object could be parsed
 			throw new InvalidFormatException(stringgetPattern(format));
 		if (!) {
 			if (string.length() != parsePosition.getIndex()) {
 				// indicates a part of the string that was not parsed; e.g. ".5" in 1234.5 when parsing an Integer
 				throw new InvalidFormatException(stringgetPattern(format));
		return convertToNumberClass(numbertargetClass);
	protected String toString(Object objectthrows Exception {
		Number number = (Numberobject;

Coerces the Number object returned by NumberFormat to the desired numberClass. Subclasses may override.

number the parsed number
the coersed number
java.lang.IllegalArgumentException when an overflow condition occurs during coersion
	protected Number convertToNumberClass(Number numberClass numberClassthrows IllegalArgumentException {
		return NumberUtils.convertNumberToTargetClass(numbernumberClass);
	// internal helpers
	private String getPattern(NumberFormat format) {
		if (format instanceof DecimalFormat) {
			return ((DecimalFormatformat).toPattern();
else {
			.warn("Pattern string cannot be determined because NumberFormat is not a DecimalFormat");
			return "defaultNumberFormatInstance";
New to GrepCode? Check out our FAQ X