litesoft
@ 947
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.core.formatters; public class NumericFormatter { private String mFormat; private NumericFormatPartHelper[] mToUndecorated; private NumericFormatPartHelper[] mToDecorated; /** * @deprecated for Serialization */ protected NumericFormatter() { } public NumericFormatter( String pFormat, // NumericFormatPartHelper[] pToUndecorated, NumericFormatPartHelper[] pToDecorated ) { mFormat = pFormat; mToUndecorated = pToUndecorated; mToDecorated = pToDecorated; } public String decorate( String pValue ) { return convert( pValue, mToDecorated ); } public String undecorate( String pValue ) { return convert( pValue, mToUndecorated ); } public String getFormat() { return mFormat; } public String toString() { return "NumericFormatter( '" + getFormat() + "' )"; } private String convert( String pValue, NumericFormatPartHelper[] pHelpers ) { NFparsedparts zParts = parseInto( pValue ); StringBuilder sb = new StringBuilder(); for ( int i = 0; i < pHelpers.length; i++ ) { NumericFormatPartHelper helper = pHelpers[i]; helper.process( sb, zParts ); } return sb.toString(); } protected NFparsedparts parseInto( String pValue ) { NFparsedparts zParts = new NFparsedparts(); if ( pValue != null ) { int dotIndex = pValue.indexOf( "." ); if ( dotIndex >= 0 ) // Dot exists { if ( (pValue.length() - 1) > dotIndex ) // Numbers after dot exist { zParts.setDecimalFound(); String beforeDot = pValue.substring( 0, dotIndex ); String afterDot = pValue.substring( dotIndex + 1 ); if ( beforeDot.length() == 0 ) { beforeDot = "0"; } if ( (beforeDot.length() == 1) && ("-".equals( beforeDot )) ) // Starts with minus-dot { if ( Integer.parseInt( afterDot ) > 0 ) { beforeDot = beforeDot + "0"; } else { beforeDot = "0"; } } parseBeforeDot( beforeDot, zParts ); parseAfterDot( afterDot, zParts ); } else { String zBeforeDot = pValue.substring( 0, dotIndex ); parseBeforeDot( zBeforeDot, zParts ); } } else { if ( pValue.startsWith( "-" ) ) { String zVal = pValue.substring( 1 ); int zIntVal = Integer.parseInt( zVal ); if ( zIntVal > 0 ) { parseBeforeDot( pValue, zParts ); } else { parseBeforeDot( zVal, zParts ); } } else if ( pValue.length() == 0 ) { parseBeforeDot( "0", zParts ); } else { parseBeforeDot( pValue, zParts ); } } } else { parseBeforeDot( "0", zParts ); } return zParts; } private void parseBeforeDot( String pBeforeDot, NFparsedparts pParts ) { char[] zChars = pBeforeDot.toCharArray(); int zFirstDigit = 0; for ( int i = 0; i < zChars.length; i++ ) { char zChar = zChars[i]; if ( Character.isDigit( zChar ) ) { zFirstDigit = i; break; } } String zBeforeDigits = pBeforeDot.substring( 0, zFirstDigit ); String zDigitStr = pBeforeDot.substring( zFirstDigit ); char[] zB4Digits = zBeforeDigits.toCharArray(); for ( int i = 0; i < zB4Digits.length; i++ ) { char zChar = zB4Digits[i]; pParts.addBeforeAnyDigits( zChar ); } char[] zDigits = zDigitStr.toCharArray(); for ( int j = 0; j < zDigits.length; j++ ) { char zDigit = zDigits[j]; pParts.addWholeDigits( zDigit ); } } private void parseAfterDot( String pAfterDot, NFparsedparts pParts ) { char[] zChars = pAfterDot.toCharArray(); for ( int i = 0; i < zChars.length; i++ ) { char zChar = zChars[i]; if ( !Character.isDigit( zChar ) ) { pParts.addAfterAnyDigits( zChar ); } else if ( Character.isDigit( zChar ) ) { pParts.addFractionalDigits( zChar ); } } } } |