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
| public static String doAmountConvertion(final String montant,
final String currencyName) {
CurrencyBean currencyDescriptor = getCurrencyDescriptor(currencyName);
String convertedCurrency = createDisplayableCurrency(montant,
currencyDescriptor);
return convertedCurrency;
}
private static final String FRENCH_DECIMAL_SEPARATOR = ",";
private static String createDisplayableCurrency(String amount,
CurrencyBean currencyDescriptor) {
String pattern = buildPattern(currencyDescriptor, amount);
Double convertibleNumber = new Double(buildConvertibleNumber(amount));
Locale local = new Locale("fr","FR");
NumberFormat formater = NumberFormat.getNumberInstance(local);
DecimalFormat decimalFormater = (DecimalFormat)formater;
decimalFormater.applyPattern(pattern);
String convertedCurrency = decimalFormater.format(convertibleNumber);
convertedCurrency = convertedCurrency.replace(FRENCH_DECIMAL_SEPARATOR, DECIMAL_SEPARATOR);
fwsLogger.debug("buildPattern : convertedCurrency=[" + convertedCurrency + "]");
StringBuffer outputBuffer = replaceSpace160BySpace32(convertedCurrency);
return outputBuffer.toString();
}
private static StringBuffer replaceSpace160BySpace32(
String convertedCurrency) {
StringBuffer outputBuffer = new StringBuffer();
for(int index=0; index<convertedCurrency.length(); index++) {
char actualChar = convertedCurrency.charAt(index);
int a = actualChar;
if(a == 160) {
outputBuffer.append(" ");
} else {
outputBuffer.append(actualChar);
}
}
return outputBuffer;
}
private static String buildConvertibleNumber(String amount) {
String convertibleNumber = "";
if (amount.contains(DECIMAL_SEPARATOR)) {
convertibleNumber = amount;
} else {
convertibleNumber = amount + DECIMAL_SEPARATOR + FILLING_ELEMENT;
}
return convertibleNumber;
}
private static final int THOUSAND_SIZE = 3;
private static final String FILLING_ELEMENT = new String("0");
private static final String INTEGER_PATTERN_BLOCS_SEPARATOR = new String(",");
private static String buildPattern(CurrencyBean currencyDescriptor,
String amount) {
StringBuffer buffer = new StringBuffer();
buffer.append(buildPatternForIntegerPart(amount));
buffer.append(DECIMAL_SEPARATOR);
buildPatternForDecimalPart(currencyDescriptor, buffer);
fwsLogger.debug("buildPattern : pattern=[" + buffer.toString() + "]");
return buffer.toString();
}
private static void buildPatternForDecimalPart(
CurrencyBean currencyDescriptor, StringBuffer buffer) {
final int decimalPartSize = currencyDescriptor.getDecimalPartSize();
for (int index = 0; index < decimalPartSize; index++) {
buffer.append(FILLING_ELEMENT);
}
}
private static String buildPatternForIntegerPart(String amount) {
StringBuffer inputBuffer = new StringBuffer();
String currentIntergerPart = getIntergerPartFromCurrencyAmount(amount);
buildReversedPattern(inputBuffer, currentIntergerPart);
StringBuffer outputBuffer = reverseBufferContent(inputBuffer);
return outputBuffer.toString();
}
private static void buildReversedPattern(StringBuffer inputBuffer,
String currentIntergerPart) {
final int patternBlocsNumberForIntergerPart = currentIntergerPart
.length()
/ THOUSAND_SIZE + 1;
// Exemple pattern : 000 000.00
for (int index = 0; index < patternBlocsNumberForIntergerPart; index++) {
if (index > 0) {
inputBuffer.append(INTEGER_PATTERN_BLOCS_SEPARATOR);
}
int addedElementNumber = buildAddedElementNumber(currentIntergerPart, index);
for (int addedElementCount = 0; addedElementCount < addedElementNumber; addedElementCount++) {
inputBuffer.append(FILLING_ELEMENT);
}
}
}
private static StringBuffer reverseBufferContent(StringBuffer inputBuffer) {
StringBuffer outputBuffer = new StringBuffer();
final int patternSize = inputBuffer.length();
int atPosition = patternSize - 1;
for(int index = 0; index < patternSize; index++) {
outputBuffer.append(inputBuffer.charAt(atPosition));
atPosition--;
}
return outputBuffer;
}
private static int buildAddedElementNumber(
final String currentIntergerPart, final int currentBlocIndex) {
int blocsnumber = currentIntergerPart.length() / THOUSAND_SIZE;
int addedElementNumber = 0;
if (currentBlocIndex < blocsnumber) {
addedElementNumber = THOUSAND_SIZE;
} else {
addedElementNumber = currentIntergerPart.length() - (blocsnumber * THOUSAND_SIZE); //blocsnumber;
}
return addedElementNumber;
}
private static final String DECIMAL_SEPARATOR = new String(".");
private static String getIntergerPartFromCurrencyAmount(String montant) {
String intergerPartFromCurrencyAmount = "";
final int separatorPosition = montant.indexOf(DECIMAL_SEPARATOR);
try {
intergerPartFromCurrencyAmount = montant.substring(0,
separatorPosition);
} catch (StringIndexOutOfBoundsException e) {
intergerPartFromCurrencyAmount = montant;
}
return intergerPartFromCurrencyAmount;
}
private static CurrencyBean getCurrencyDescriptor(final String currencyName) {
fwsLogger.debug("getCurrencyDescriptor : currencyName=[" + currencyName
+ "]");
return CurrencyFactory.getUniqueInstance().buildCurrency(currencyName);
} |
Partager