View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Power Of Two S.R.L. (www.pow2.com)
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   */
23  
24  package com.pow2.util;
25  
26  
27  import java.text.*;
28  
29  import org.apache.log4j.Category;
30  
31  
32  /***
33   *  NumberUtil class.
34   *  <br>
35   *  Really simple numer / currency utility class. To do a better job,
36   *  I should include a real Currency class. However, coders are lazy...
37   *
38   * @author  Luca Fossato
39   * @created  18 June 2002
40   */
41  public class NumberUtil
42  {
43    /*** log4j category */
44    private final static Category cat = Category.getInstance(NumberUtil.class);
45  
46  
47    /***
48     *  Get the float value from an Euro currency string representation.
49     *  <br>
50     *  Note: this method uses the current Locale settings.
51     *
52     * @param  value the Euro currency string representation
53     * @return the float value from an Euro currency string representation
54     * @exception ParseException if any error occurs
55     */
56    public static float euroCurrencyToFloat(String value) throws ParseException
57    {
58      NumberFormat nf = NumberFormat.getNumberInstance();
59      return nf.parse(value).floatValue();
60    }
61  
62  
63    /***
64     *  Get the string representation of the Euro currency value.
65     *
66     * @param  value the Object representing the value to parse
67     * @return the string representation of the Euro currency value, or null
68     *         if the conversion fails
69     */
70    public static String getEuroCurrency(Object value)
71    {
72      return getCurrency(value, "##,##0.00");
73    }
74  
75  
76    /***
77     *  Get the string representation of a currency value.
78     *
79     * @param  value the Object representing the value to parse
80     * @param  pattern the string pattern to use to parse the input value object
81     * @return the string representation of a currency value, or null
82     *         if the conversion fails
83     */
84    public static String getCurrency(Object value, String pattern)
85    {
86      String res = null;
87      DecimalFormat formatter = new DecimalFormat(pattern);
88  
89      try
90      {
91        res = formatter.format(value);
92      }
93      catch (java.lang.IllegalArgumentException e)
94      {
95        cat.error("::getCurrency - format exception:", e);
96      }
97  
98      return res;
99    }
100 }