| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.util; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Utility methods around Numbers. |
|---|
| 38 | * |
|---|
| 39 | * Mainly for BigDecimals, used by the restricted number |
|---|
| 40 | * strong types. |
|---|
| 41 | * |
|---|
| 42 | * @author Andreas Mandel |
|---|
| 43 | */ |
|---|
| 44 | public final class NumberUtil |
|---|
| 45 | { |
|---|
| 46 | private static final int LONG_MIN_VALUE_DIGITS = 19; |
|---|
| 47 | private static final long[] DIGITS = |
|---|
| 48 | { |
|---|
| 49 | 0L, |
|---|
| 50 | 10L, |
|---|
| 51 | 100L, |
|---|
| 52 | 1000L, |
|---|
| 53 | 10000L, |
|---|
| 54 | 100000L, |
|---|
| 55 | 1000000L, |
|---|
| 56 | 10000000L, |
|---|
| 57 | 100000000L, |
|---|
| 58 | 1000000000L, |
|---|
| 59 | 10000000000L, |
|---|
| 60 | 100000000000L, |
|---|
| 61 | 1000000000000L, |
|---|
| 62 | 10000000000000L, |
|---|
| 63 | 100000000000000L, |
|---|
| 64 | 1000000000000000L, |
|---|
| 65 | 10000000000000000L, |
|---|
| 66 | 100000000000000000L, |
|---|
| 67 | 1000000000000000000L |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | private static final char [] ZEROS |
|---|
| 71 | = "000000000000000000".toCharArray(); |
|---|
| 72 | |
|---|
| 73 | private NumberUtil () |
|---|
| 74 | { |
|---|
| 75 | // no instances |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Counts the number of decimal digits needed to represent |
|---|
| 80 | * the given value. |
|---|
| 81 | * @param value the value to count for. |
|---|
| 82 | * @return the number of decimal digits needed to represent |
|---|
| 83 | * the given value. |
|---|
| 84 | */ |
|---|
| 85 | public static int countDigits (long value) |
|---|
| 86 | { |
|---|
| 87 | int result; |
|---|
| 88 | |
|---|
| 89 | if (value == Long.MIN_VALUE) |
|---|
| 90 | { |
|---|
| 91 | result = LONG_MIN_VALUE_DIGITS; |
|---|
| 92 | } |
|---|
| 93 | else |
|---|
| 94 | { |
|---|
| 95 | final long test = Math.abs(value); |
|---|
| 96 | for (result = 0; result < DIGITS.length; result++) |
|---|
| 97 | { |
|---|
| 98 | if (test < DIGITS[result]) |
|---|
| 99 | { |
|---|
| 100 | break; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | return result; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Convert the given unscaled long with the given scale to |
|---|
| 109 | * it's string representation. |
|---|
| 110 | * @param unscaledValue the scaled long. |
|---|
| 111 | * @param scale the scale to be applied. |
|---|
| 112 | * @return string representation of the scaled value using a dot |
|---|
| 113 | * '.' as decimal separator. |
|---|
| 114 | */ |
|---|
| 115 | public static String toString (long unscaledValue, int scale) |
|---|
| 116 | { |
|---|
| 117 | Assert.assertTrue("Scale must not be negative.", scale >= 0); |
|---|
| 118 | final StringBuffer sb = new StringBuffer(); |
|---|
| 119 | sb.append(Math.abs(unscaledValue)); |
|---|
| 120 | final int missingDigits = 1 + scale - sb.length(); |
|---|
| 121 | if (missingDigits > 0) |
|---|
| 122 | { |
|---|
| 123 | sb.insert(0, ZEROS, 0, 1 + scale - sb.length()); |
|---|
| 124 | } |
|---|
| 125 | if (scale > 0) |
|---|
| 126 | { |
|---|
| 127 | sb.insert(sb.length() - scale, '.'); |
|---|
| 128 | } |
|---|
| 129 | if (unscaledValue < 0) |
|---|
| 130 | { |
|---|
| 131 | sb.insert(0, '-'); |
|---|
| 132 | } |
|---|
| 133 | return sb.toString(); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|