KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > NumberUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: NumberUtils.java,v 1.9 2007/01/07 06:14:01 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.text.NumberFormat JavaDoc;
25
26 /**
27  * Collection of useful utilities to work with numbers.
28  *
29  * @version $Id: NumberUtils.java,v 1.9 2007/01/07 06:14:01 bastafidli Exp $
30  * @author Peter Satury
31  * @code.reviewer Miro Halas
32  * @code.reviewed Initial revision
33  */

34 public final class NumberUtils
35 {
36    // Constants ////////////////////////////////////////////////////////////////
37

38    /**
39     * Static array used to append leading 0 chars to file name constructed from
40     * number.
41     */

42    protected static final char[] ZEROCHARS = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
43                                               '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
44                                               '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
45                                               '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
46                                               '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
47                                              };
48
49    // Cached values ////////////////////////////////////////////////////////////
50

51    /**
52     * static Number format for no Exponent
53     */

54    public static final NumberFormat JavaDoc NFFORMAT;
55    
56    /**
57     * static Number format for no Exponent for editing
58     */

59    public static final NumberFormat JavaDoc NFFORMATEDIT;
60
61    /**
62     * static Number format for Currency
63     */

64    public static final NumberFormat JavaDoc NFCURRENCYFORMAT;
65
66    /**
67     * static Number format for Currency for editing
68     */

69    public static final NumberFormat JavaDoc NFCURRENCYFORMATEDIT;
70
71    // Constructors /////////////////////////////////////////////////////////////
72

73    /**
74     * Static initializer.
75     */

76    static
77    {
78       
79       NFFORMAT = NumberFormat.getNumberInstance();
80       NFFORMAT.setMaximumFractionDigits(20);
81       
82       NFFORMATEDIT = NumberFormat.getNumberInstance();
83       NFFORMATEDIT.setMaximumFractionDigits(20);
84       NFFORMATEDIT.setGroupingUsed(false);
85
86       NFCURRENCYFORMAT = NumberFormat.getNumberInstance();
87       NFCURRENCYFORMAT.setMaximumFractionDigits(2);
88       NFCURRENCYFORMAT.setMinimumFractionDigits(2);
89
90       NFCURRENCYFORMATEDIT = NumberFormat.getNumberInstance();
91       NFCURRENCYFORMATEDIT.setMaximumFractionDigits(2);
92       NFCURRENCYFORMATEDIT.setMinimumFractionDigits(2);
93       NFCURRENCYFORMATEDIT.setGroupingUsed(false);
94
95    }
96
97    /**
98     * Private constructor since this class cannot be instantiated
99     */

100    private NumberUtils(
101    )
102    {
103       // Do nothing
104
}
105
106    // Public methods ///////////////////////////////////////////////////////////
107

108    /**
109     * Method to make Exponention
110     *
111     * @param iBbase - base of Exponention [1..]
112     * @param iExponent - exponent of Exponention [0..14]
113     * @return long - result of Exponention
114     * @throws IllegalArgumentException - in case of arguments out of valid range
115     */

116    public static long exponentiate(
117       int iBbase,
118       int iExponent
119    ) throws IllegalArgumentException JavaDoc
120    {
121       if (iExponent > 14 || iExponent < 0)
122       {
123          throw new IllegalArgumentException JavaDoc(
124                "Exponent could not be greater then 14 and lower then 0");
125       }
126       if (iBbase < 1)
127       {
128          throw new IllegalArgumentException JavaDoc(
129                "Exponentiate base could not be lower then 1");
130       }
131       long lReturn = 1;
132       for (int iCounter = 0; iCounter < iExponent; iCounter++)
133       {
134          try
135          {
136             lReturn = lReturn * iBbase;
137          }
138          catch (Exception JavaDoc eExc)
139          {
140             throw new IllegalArgumentException JavaDoc(
141                   "Exponentiate arguments too high");
142          }
143       }
144       return lReturn;
145    }
146
147    /**
148     * Method to make specified length digit number string representation from particular
149     * input number.
150     * For example, if there will be send input number 32 and digit lenhth = 8, output
151     * will be string '00000032'
152     *
153     * @param iInputNumber - input number that will be converted into 8 digit number
154     * string representation
155     * @param iDigitLength - length of the output digit number string
156     *
157     * @return String - digit number string representation
158     */

159    public static String JavaDoc getDigitNumberString(
160       int iInputNumber,
161       int iDigitLength
162    )
163    {
164       StringBuffer JavaDoc idString = new StringBuffer JavaDoc(Integer.toString(iInputNumber));
165       
166       if (iDigitLength - idString.length() > 0)
167       {
168          idString.insert(0, ZEROCHARS, 0, iDigitLength - idString.length());
169       }
170
171       return idString.toString();
172    }
173 }
174
Popular Tags