Saturday, February 16, 2008

Currency in words conversion Algorithm

In one of my job interviews, I was asked to write a program to convert the currency (in numbers) to currency in words. Althought I could not come up with the correct algo at that time, but later wrote it a java program for it. Since I could not find any other algorithm on the internet for the same, I thought it would be useful for others if I post my work here. I know this algorithm is not the most efficient one so if you have a better algo PLEASE let me know. Also if you see any bug please let me know. Any help/suggestions/comments are much appreciated !!

Problem Statement : Convert a given number words. E.g. the number 4,132,013 should return Four Million One hundred thirty two thousand thirteen

Solution : I have used two classes. The class CurrencyUtil is just a utility class providing helper methods to extract digits and count number of digits etc. CurrencyAlgo has the code for conversion. Finally one last class to test the code

CurrencyAlgo.java

import java.util.*;

public class CurrencyAlgo extends CurrencyUtil{
public String in_words(long number){
int counter = 0;
String words= "";
String finalWords= " Only";

if(!hasMoreDigits(number))
finalWords = "Zero";
else{
while(hasMoreDigits(number)){
words = getInWords(get_last_three(number));
if (words.indexOf("Zero")==-1)
finalWords = words + big_nums[counter] + finalWords;
counter++;
number = (number - get_last_three(number))/1000;
}
}
return finalWords;
}

private static String getInWords(long num){
int digits = no_of_digits(num);

if(digits<=2 && get_second_last(num) <2){ digits="="2">=2){
String unit = ones.get(Integer.toString(get_last(num)));
String unitValue = unit.equalsIgnoreCase(" zero") ? "" : unit;

return hundreds.get(Integer.toString(get_second_last(num))) + unitValue;
}else{
return ones.get(Integer.toString(get_third_last(num))) +
" hundred" + getInWords(get_last_two(num));
}
}
}


CurrencyUtil.java

import java.util.*;

public class CurrencyUtil{

final static String[] big_nums = {"", " thousand"," million"," billion", "trillion"};
static HashMap ones = new HashMap();
static HashMap hundreds = new HashMap();
public static boolean hasMoreDigits(long num){
return (num != 0);
}

public static int get_last_three(long num){
return (int)num%1000;
}

public static int get_last_two(long num){
return (int)num%100;
}

public static int get_last(long num){
return (int)num%10;
}

public static int get_second_last(long num){
int sec_last = (get_last_two(num) - get_last(num))/10;
return sec_last;
}

public static int get_third_last(long num){
int third_last = (get_last_three(num) - get_last_two(num))/100;
return third_last;
}

public static int no_of_digits(long num){
int no=0;
while(num > 0){
num= (num-num%10)/10;
no++;
}
return no;
}

public static void setup(){

ones.put("0"," Zero");
ones.put("1"," One");
ones.put("2"," two");
ones.put("3"," three");
ones.put("4"," four");
ones.put("5"," five");
ones.put("6"," six");
ones.put("7"," seven");
ones.put("8"," eight");
ones.put("9"," nine");
ones.put("10"," ten");
ones.put("11"," eleven");
ones.put("12"," twelve");
ones.put("13"," thirteen");
ones.put("14"," fourteen");
ones.put("15"," fifteen");
ones.put("16"," sixteen");
ones.put("17"," seventeen");
ones.put("18"," eighteen");
ones.put("19"," ninteen");

hundreds.put("2"," twenty");
hundreds.put("3"," thirty");
hundreds.put("4"," fourty");
hundreds.put("5"," fifty");
hundreds.put("6"," sixty");
hundreds.put("7"," seventy");
hundreds.put("8"," eighty");
hundreds.put("9"," ninty");

}

public static void teardown(){
ones = null;
hundreds = null;
}

}

CurrencyAlgoTest.java

import java.util.*;

public class CurrencyAlgoTest{

public static void main(String[] args){

int[] single_digit = {0, 1, 2, 3, 4 ,5, 6, 7, 8, 9};
int[] two_digit = {10, 11, 12, 13, 14 ,15, 16, 17, 18, 19, 20};
long[] test_int = new long[10];

for(int n=0; n < test_int.length;>
test_int[n]=get_billion_rand();
}
test_int[0]=10001;

CurrencyAlgo ca = new CurrencyAlgo();
ca.setup();
for(int i=0;i
System.out.println("number "+test_int[i]+" in words : "+ ca.in_words(test_int[i]));
}
ca.teardown();
}

private static int get_ten_rand(){
return (int) (Math.random()*11);
}

private static long get_billion_rand(){
long tmp = (long) Math.pow((double)10 , (double)get_ten_rand());
long tmp2 = (long) Math.pow((double)10, (double)5);
return (long) (Math.random()*tmp2);
}
}

No comments: