我正在读取文件中的行数。我有这个作为双重的可能最后一行未完成。我需要输出行数作为一串单词。什么是一个好方法呢?
Double 850.25
去 "第五十五和第四行"
答案 0 :(得分:0)
从double到String:
String doubleInString = "" + double;
或者更愿意这样做:
String.valueOf(double)
@DwB
是你想要的吗?
从String到double:
try
{
Double.parseDouble(myString)
}catch( Exception e )
{
// what you want in case of error
}
从双重到单词:
你可以使用RuleBasedNumberFormat,但我从未尝试过这么好的运气:)
答案 1 :(得分:0)
这是你的答案。认为这可能会有所帮助。
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);
}
}
答案 2 :(得分:-2)
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(" ", " ");
}