1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }