HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

Convert an Integer number to readable format in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
formatnumberreadableconvertjavainteger

Problem

The number is ranging from 1 to 999999999.

```
public class ConvertIntegertoEnglish {

private String getCountSuffix(int value){
String word = "";
switch(value){
case 3:
word = "Hundred "; return word;
case 4:
word = "Thousand "; return word;
case 5:
word = "Thousand "; return word;
case 6:
word = "Lac "; return word;
case 7:
word = "Lacs "; return word;
case 8:
word = "Crore "; return word;
case 9:
word = "Crores "; return word;
default:
return "";
}

}

private String getCountPrefix(String count){
String word = "";
int value = Integer.valueOf(count);

if(value >=20 && value =30 && value =40 && value =50 && value =60 && value =70 && value =80 && value =90 && value 0;i--){
// 00 00 00 000
int n = len - i;
if((i%2)>0 && i>3 ){
temp = number.substring(n,(n + 2));
int v = Integer.parseInt(temp);
if(v 19){
temp = temp.substring(1);
sentence = sentence + getCountPrefix(temp);
}
sentence = sentence + getCountSuffix(i);
i--;
}else if((i%2)==0 && i>3){
temp = number.substring(n,(n + 1));
sentence = sentence + getCountPrefix(temp);
sentence = sentence + getCountSuffix(i);
}else if(i == 3){
temp = number.substring(n,(n + 1));
if(temp.startsWith("0")) continue;
sentence = sentence + getCountPrefix(temp);
sentence = sentence + getCountSuffix(3);
}else{
if(i == 2)
temp = number.substring(len-2);
else
temp = number.substring(len-1);
int v = Integer.parseInt(temp

Solution

There is no point in instantiating the class. All the methods could be static (and the constructor private) to provide the same functionality.

The goal is to map some String values to certain int values. A HashMap provides exactly that functionality.

import java.util.HashMap;

public final class Converter
{
    private static final HashMap suffixes;
    private static final HashMap prefixes;

    static
    {
        suffixes = new HashMap();
        suffixes.put(3, "Hundred ");
        suffixes.put(4, "Thousand ");
        suffixes.put(5, "Thousand ");
        suffixes.put(6, "Lac ");
        suffixes.put(7, "Lacs ");
        suffixes.put(8, "Crore ");
        suffixes.put(9, "Crores ");

        prefixes = new HashMap();
        prefixes.put(0, "");
        prefixes.put(1, "One ");
        prefixes.put(2, "Two ");
        prefixes.put(3, "Three ");
        prefixes.put(4, "Four ");
        prefixes.put(5, "Five ");
        prefixes.put(6, "Six ");
        prefixes.put(7, "Seven ");
        prefixes.put(8, "Eight ");
        prefixes.put(9, "Nine ");
        prefixes.put(10, "Ten ");
        prefixes.put(11, "Eleven ");
        prefixes.put(12, "Tweleve ");
        prefixes.put(13, "Thirteen ");
        prefixes.put(14, "Fourteen ");
        prefixes.put(15, "Fifteen ");
        prefixes.put(16, "Sixteen ");
        prefixes.put(17, "Seveneteen ");
        prefixes.put(18, "Eighteen ");
        prefixes.put(19, "Nineteen ");

        // decades
        prefixes.put(20, "Twenty ");
        prefixes.put(30, "Thirty ");
        prefixes.put(40, "Fourty ");
        prefixes.put(50, "Fifty ");
        prefixes.put(60, "Sixty ");
        prefixes.put(70, "Seventy ");
        prefixes.put(80, "Eighty ");
        prefixes.put(90, "Ninty ");
    }


There's only one entry for each decade, which means some functionality is required to round values to the lower bound of the possible range. Alternatively, you could add all values of the range as entries to the HashMap. The methods to access both maps could look like this:

private static String getCountSuffix(int value)
{
    return suffixes.containsKey(value) ? suffixes.get(value) : "";
}

private static String getCountPrefix(String count)
{
    int value = Integer.valueOf(count);

    if (prefixes.containsKey(value))
    {
        return prefixes.get(value);
    }

    // to decade
    value = (int)Math.floor(value/10)*10;

    if (prefixes.containsKey(value))
    {
        return prefixes.get(value);
    }

    return "";
}


Having the mapping in a data structure like that makes it a lot easier to transition to non-static code if need be. If you want to create several converters for different languages, you could create them by exchanging the used maps.

The method convertIntoEnglish that does the conversion is a pain to read, so much in fact that I exclude it from this review. As a general advice, iInstead of concatenating to a String with +: sentence = sentence + getCountPrefix(temp);, use StringBuilder.

Code Snippets

import java.util.HashMap;

public final class Converter
{
    private static final HashMap<Integer, String> suffixes;
    private static final HashMap<Integer, String> prefixes;

    static
    {
        suffixes = new HashMap<Integer, String>();
        suffixes.put(3, "Hundred ");
        suffixes.put(4, "Thousand ");
        suffixes.put(5, "Thousand ");
        suffixes.put(6, "Lac ");
        suffixes.put(7, "Lacs ");
        suffixes.put(8, "Crore ");
        suffixes.put(9, "Crores ");

        prefixes = new HashMap<Integer, String>();
        prefixes.put(0, "");
        prefixes.put(1, "One ");
        prefixes.put(2, "Two ");
        prefixes.put(3, "Three ");
        prefixes.put(4, "Four ");
        prefixes.put(5, "Five ");
        prefixes.put(6, "Six ");
        prefixes.put(7, "Seven ");
        prefixes.put(8, "Eight ");
        prefixes.put(9, "Nine ");
        prefixes.put(10, "Ten ");
        prefixes.put(11, "Eleven ");
        prefixes.put(12, "Tweleve ");
        prefixes.put(13, "Thirteen ");
        prefixes.put(14, "Fourteen ");
        prefixes.put(15, "Fifteen ");
        prefixes.put(16, "Sixteen ");
        prefixes.put(17, "Seveneteen ");
        prefixes.put(18, "Eighteen ");
        prefixes.put(19, "Nineteen ");

        // decades
        prefixes.put(20, "Twenty ");
        prefixes.put(30, "Thirty ");
        prefixes.put(40, "Fourty ");
        prefixes.put(50, "Fifty ");
        prefixes.put(60, "Sixty ");
        prefixes.put(70, "Seventy ");
        prefixes.put(80, "Eighty ");
        prefixes.put(90, "Ninty ");
    }
private static String getCountSuffix(int value)
{
    return suffixes.containsKey(value) ? suffixes.get(value) : "";
}

private static String getCountPrefix(String count)
{
    int value = Integer.valueOf(count);

    if (prefixes.containsKey(value))
    {
        return prefixes.get(value);
    }

    // to decade
    value = (int)Math.floor(value/10)*10;

    if (prefixes.containsKey(value))
    {
        return prefixes.get(value);
    }

    return "";
}

Context

StackExchange Code Review Q#129610, answer score: 4

Revisions (0)

No revisions yet.