View Javadoc

1   /*
2    * Copyright 2006 Christian Kalkhoff <me@ninan.info>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.plausj.bank.german.util;
18  
19  /**
20   * Some mathematical helper functions. They are called statically
21   * 
22   * @author ninan
23   * 
24   */
25  public class MathHelper {
26  
27  	/**
28  	 * Prevents the class from being instanciated
29  	 * 
30  	 */
31  	private MathHelper() {
32  		throw new IllegalStateException("This CTOR shouldn't be ever called");
33  	}
34  
35  	/**
36  	 * Calculates the cross total of the supplied integer
37  	 * 
38  	 * @param aNumber
39  	 *            The integer to calc the cross total from
40  	 * @return The cross total of the supplied integer
41  	 */
42  	public static int calcCrossTotal(final int aNumber) {
43  		int result = 0;
44  
45  		int x = aNumber;
46  
47  		while (x > 0) {
48  			result += x % 10;
49  			x = x / 10;
50  		}
51  
52  		return result;
53  	}
54  
55  	/**
56  	 * Returns the last "count" digits of the supplied number as a new integer
57  	 * 
58  	 * @param aNumber
59  	 *            The number to return the digits from
60  	 * @param count
61  	 *            The count (from the right) of how many digits to return
62  	 * @return "count" digits of "aNumber"
63  	 */
64  	public static int getLastDigits(final int aNumber, final int count) {
65  
66  		int n = aNumber;
67  		int result = 0;
68  		int factor = 1;
69  
70  		for (int i = 0; i < count && n > 0; ++i) {
71  			result = result + ((n % 10) * factor);
72  			n = n / 10;
73  			factor = factor * 10;
74  		}
75  
76  		return result;
77  
78  	}
79  
80  }