| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | |
| 36 | | | |
| 37 | | | |
| 38 | | | |
| 39 | | | |
| 40 | | | |
| 41 | | | |
| 42 | | | @author |
| 43 | | | |
| 44 | | | public final class NumberUtil |
| 45 | | | { |
| 46 | | | private static final int LONG_MIN_VALUE_DIGITS = 19; |
| 47 | 100 | | 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 | 100 | | private static final char [] ZEROS |
| 71 | | | = "000000000000000000".toCharArray(); |
| 72 | | | |
| 73 | | | private NumberUtil () |
| 74 | 0 | | { |
| 75 | | | |
| 76 | 0 | | } |
| 77 | | | |
| 78 | | | |
| 79 | | | |
| 80 | | | |
| 81 | | | @param |
| 82 | | | @return |
| 83 | | | |
| 84 | | | |
| 85 | | | public static int countDigits (long value) |
| 86 | | | { |
| 87 | | | int result; |
| 88 | | | |
| 89 | 100 | | if (value == Long.MIN_VALUE) |
| 90 | | | { |
| 91 | 0 | | result = LONG_MIN_VALUE_DIGITS; |
| 92 | | | } |
| 93 | | | else |
| 94 | | | { |
| 95 | 100 | | final long test = Math.abs(value); |
| 96 | 100 | | for (result = 0; result < DIGITS.length; result++) |
| 97 | | | { |
| 98 | 100 | | if (test < DIGITS[result]) |
| 99 | | | { |
| 100 | 100 | | break; |
| 101 | | | } |
| 102 | | | } |
| 103 | | | } |
| 104 | 100 | | return result; |
| 105 | | | } |
| 106 | | | |
| 107 | | | |
| 108 | | | |
| 109 | | | |
| 110 | | | @param |
| 111 | | | @param |
| 112 | | | @return |
| 113 | | | |
| 114 | | | |
| 115 | | | public static String toString (long unscaledValue, int scale) |
| 116 | | | { |
| 117 | 75 | | Assert.assertTrue("Scale must not be negative.", scale >= 0); |
| 118 | 100 | | final StringBuffer sb = new StringBuffer(); |
| 119 | 100 | | sb.append(Math.abs(unscaledValue)); |
| 120 | 100 | | final int missingDigits = 1 + scale - sb.length(); |
| 121 | 100 | | if (missingDigits > 0) |
| 122 | | | { |
| 123 | 100 | | sb.insert(0, ZEROS, 0, 1 + scale - sb.length()); |
| 124 | | | } |
| 125 | 100 | | if (scale > 0) |
| 126 | | | { |
| 127 | 100 | | sb.insert(sb.length() - scale, '.'); |
| 128 | | | } |
| 129 | 100 | | if (unscaledValue < 0) |
| 130 | | | { |
| 131 | 100 | | sb.insert(0, '-'); |
| 132 | | | } |
| 133 | 100 | | return sb.toString(); |
| 134 | | | } |
| 135 | | | } |