2 * Copyright 2000-2018 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.v7.data.util.converter;
18 import java.math.BigDecimal;
19 import java.text.DecimalFormat;
20 import java.text.NumberFormat;
21 import java.util.Locale;
24 * A converter that converts from {@link String} to {@link BigDecimal} and back.
25 * Uses the given locale and a {@link NumberFormat} instance for formatting and
28 * Leading and trailing white spaces are ignored when converting from a String.
31 * Override and overwrite {@link #getFormat(Locale)} to use a different format.
38 public class StringToBigDecimalConverter
39 extends AbstractStringToNumberConverter<BigDecimal> {
41 protected NumberFormat getFormat(Locale locale) {
42 NumberFormat numberFormat = super.getFormat(locale);
43 if (numberFormat instanceof DecimalFormat) {
44 ((DecimalFormat) numberFormat).setParseBigDecimal(true);
51 public BigDecimal convertToModel(String value,
52 Class<? extends BigDecimal> targetType, Locale locale)
53 throws ConversionException {
54 return (BigDecimal) convertToNumber(value, BigDecimal.class, locale);
58 public Class<BigDecimal> getModelType() {
59 return BigDecimal.class;