To convert currency from number to string format in Java


Here is a code :



import java.util.Locale;
import java.util.regex.Pattern;


/**
 * CurrencyConverter is used to convert the given amount string into words
 *
 * @author Ravi Sharma
 */
public class CurrencyConverter {
/** String array of words for tens Names */
private static final String[] tensNames = { "", "", "TWENTY", "THIRTY",
"FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" };

/** String array of words for ones Names */
private static final String[] onesNames = { "", "ONE", "TWO", "THREE",
"FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN",
"TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN",
"SEVENTEEN", "EIGHTEEN", "NINETEEN" };

/**
* This method converts the given amount in string into words
*
* @param strAmount
*            String Amount to be converted
*
* @return String Converted amount in words
*
* @throws Exception
*/
public static String converter(String strAmount) throws Exception {
String output = "";

try {
String totalAmount = strAmount;
int index = totalAmount.indexOf(".");

String beforeDecimal = totalAmount;

if (index > -1) {
beforeDecimal = totalAmount.substring(0, index);
}

output = evaluate(beforeDecimal) + " RUPEES ";

if (index > -1) {
String afterDecimal = totalAmount.substring((index + 1));
output += (" AND " + evaluate(afterDecimal) + " PAISE");
}
} catch (Exception exception) {
System.out.println("Error in CurrencyConverter.converter(String aStrAmount) method");
System.out.println(exception);
}

return output;
}

/**
* This method converts the given amount into words
*
* @param iAmount
*            String Amount
*
* @return String output
*
* @throws Exception
*/
public static String converter(int iAmount) throws Exception {
String output = "";
Integer i = null;
try {
i = new Integer(iAmount);
output = converter(i.toString());
} catch (Exception exception) {
System.out
.println("Error in CurrencyConverter.converter(int iAmount) method");
throw exception;
}
return output;
}

/**
* This method is used to validate the number in string format
*
* @param text
*            String
*
* @return String
*
* @throws Exception
*/
private static String evaluate(String text) throws Exception {
long number = 0;

try {
number = Long.parseLong(text);
} catch (NumberFormatException eNumberFormatException) {
System.out
.println("Error in CurrencyConverter.evaluate(String text) method");
throw eNumberFormatException;
}

return evaluate(number);
}

/**
* This method is used to compute the number in words
*
* @param number
*            long
*
* @return String
*/
private static String evaluate(long number) {
long temp = number;

long crore = temp / 10000000;
temp %= 10000000;

long lakh = temp / 100000;
temp %= 100000;

long thousands = temp / 1000;
temp %= 1000;

long hundreds = temp / 100;
temp %= 100;

StringBuffer result = new StringBuffer(30);

if (crore > 0) {
result.append(evaluate(crore) + " CRORE ");
}

if (lakh > 0) {
result.append(evaluate(lakh) + " LAKH ");
}

if (thousands > 0) {
result.append(evaluate(thousands) + " THOUSAND ");
}

if (hundreds > 0) {
result.append(evaluate(hundreds) + " HUNDRED ");
}

if (temp != 0) {
if (number >= 100) {
result.append("AND ");
}

if ((0 < temp) && (temp <= 19)) {
result.append(onesNames[(int) temp]);
} else {
long tens = temp / 10;
long ones = temp % 10;
result.append(tensNames[(int) tens] + " ");
result.append(onesNames[(int) ones]);
}
}

if ((result.toString()).trim().equals("")) {
result.append(" ZERO ");
}

return result.toString();
}


}

Comments

Popular posts from this blog

Java 21 Features With Example

Java 10 and 10 Big Java Milestones