I am reading the number of lines in a file. I have this coming as a double by that maybe the last line is not complete. I need to output the number of lines as a string of words. What would be a good way to do this?

Double 850.25

goes to "Eight hundred and fifty and a fourth lines"

有帮助吗?

解决方案

This is your answer. think it could be helpful.

public class CashWordConverter {

public static final String[] units = {
    "", " one", "two", "three", "four", "five", "six", "seven",
    "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
    "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};

public static final String[] tens = {
    "", // 0
    "", // 1
    "twenty", // 2
    "thirty", // 3
    "forty", // 4
    "fifty", // 5
    "sixty", // 6
    "seventy", // 7
    "eighty", // 8
    "ninety" // 9
};

public static String doubleConvert(final double n) {
    String pass = n + "";
    StringTokenizer token = new StringTokenizer(pass, ".");
    String first = token.nextToken();
    String last = token.nextToken();
    try {
        pass = convert(Integer.parseInt(first))+" ";
        pass=pass+"POINT";
        for (int i = 0; i < last.length(); i++) {
            String get=convert(Integer.parseInt(last.charAt(i)+""));
            if(get.isEmpty()){
            pass=pass+" "+"ZERO";
            }else{
            pass=pass+" "+get;    
            }
        }

    } catch (NumberFormatException nf) {
    }
    return pass;
}

public static String convert(final int n) {
    if (n < 0) {
        return "minus " + convert(-n);
    }

    if (n < 20) {
        return units[n];
    }

    if (n < 100) {
        return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
    }

    if (n < 1000) {
        return units[n / 100] + " hundred " + ((n % 100 != 0) ? " " : "") + convert(n % 100);
    }

    if (n < 1000000) {
        return convert(n / 1000) + " thousand " + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
    }

    if (n < 1000000000) {
        return convert(n / 1000000) + " million " + ((n % 1000000 != 0) ? " " : "") + convert(n % 1000000);
    }

    return convert(n / 1000000000) + " billion " + ((n % 1000000000 != 0) ? " " : "") + convert(n % 1000000000);
  }
}

其他提示

From double to String :

String doubleInString = "" + double;

Or more intended to do this :

String.valueOf(double) @DwB

is that what you want ?

From String to double :

try
{
    Double.parseDouble(myString)
}catch( Exception e )
{
   // what you want in case of error
}

From double to words :

You can use RuleBasedNumberFormat but I never tried so good luck :)

Update (Mahbubur Rahman Khan) response,
Conversion of the second part will be better.   

public static String doubleConvert(final double n) {
            if (n == 0d) {
                return "0.0";
            }
            String          pass  = n + "";
            StringTokenizer token = new StringTokenizer(pass, ".");
            String          first = token.nextToken();
            String          last  = token.nextToken();
            try {
                pass = convert(Integer.parseInt(first)) + " ";
                pass = pass + "point";
                if (Integer.parseInt(last) > 0){
                    pass = pass + " " + convert(Integer.parseInt(last));
                } else {
                    pass = pass + " " + "zero";
                }
            } catch (NumberFormatException nf) {
                // error infos
            }
            return pass.replace("  ", " ");
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top