1041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus/**
2041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * Copyright (C) 2010 the original author or authors.
3041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * See the notice.md file distributed with this work for additional
4041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * information regarding copyright ownership.
5041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus *
6041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * Licensed under the Apache License, Version 2.0 (the "License");
7041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * you may not use this file except in compliance with the License.
8041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * You may obtain a copy of the License at
9041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus *
10041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus *     http://www.apache.org/licenses/LICENSE-2.0
11041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus *
12041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * Unless required by applicable law or agreed to in writing, software
13041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * distributed under the License is distributed on an "AS IS" BASIS,
14041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * See the License for the specific language governing permissions and
16041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * limitations under the License.
17041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus */
18041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus
19041bdac9067d4ce9c936ff1c0ae3856f2e240f15Anguspackage com.beust.jcommander.converters;
20041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus
21041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angusimport com.beust.jcommander.ParameterException;
223e7cb52cbf4ca537cafb6617b20387225672ad1aCedric Beust
23041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angusimport java.math.BigDecimal;
24041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus
25041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus/**
26041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus * Converts a String to a BigDecimal.
27041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus *
28b02a7c5b3388b64a133847d3630450f099e6e141Angus * @author Angus Smithson
29041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus */
30041bdac9067d4ce9c936ff1c0ae3856f2e240f15Anguspublic class BigDecimalConverter extends BaseConverter<BigDecimal> {
31041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus
32041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus  public BigDecimalConverter(String optionName) {
33041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus    super(optionName);
34041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus  }
35041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus
36041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus  public BigDecimal convert(String value) {
37041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus    try {
38041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus      return new BigDecimal(value);
39041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus    } catch (NumberFormatException nfe) {
40041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus      throw new ParameterException(getErrorString(value, "a BigDecimal"));
41041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus    }
42041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus  }
43041bdac9067d4ce9c936ff1c0ae3856f2e240f15Angus}
44