我只想使用 Dart 作为语言将整数
55
转换为单词形式 "fifty-five"
。另一个例子是
int nums = 10294;
// convert it to 'ten thousand two hundred ninety four'
看起来这个问题之前一定已经解决了。
我会采取以下步骤:
const zero = "zero";
const oneToNine = [
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
];
const tenToNinteen = [
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
];
const dozens = [
"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
];
String solution(int number) {
if(number == 0) {
return zero;
}
return generate(number).trim();
}
String generate(int number) {
if(number >= 1000000000) {
return generate(number ~/ 1000000000) + " billion " + generate(number % 1000000000);
}
else if(number >= 1000000) {
return generate(number ~/ 1000000) + " million " + generate(number % 1000000);
}
else if(number >= 1000) {
return generate(number ~/ 1000) + " thousand " + generate(number % 1000);
}
else if(number >= 100) {
return generate(number ~/ 100) + " hundred " + generate(number % 100);
}
return generate1To99(number);
}
String generate1To99(int number) {
if (number == 0) {
return "";
}
else if (number <= 9) {
return oneToNine[number - 1];
}
else if (number <= 19) {
return tenToNinteen[number % 10];
}
else {
return dozens[number ~/ 10 - 1] + " " + generate1To99(number % 10);
}
}
void main() {
print(solution(10294));
print(solution(1234567890));
}
本质上,您需要每个数字的字面等值,并且它们应该按其范围(1000、100、10、1 等)排序。以下代码生成 55,但您需要将其调整为 10294 并添加一些特殊情况(数字 10 到 19 可能必须进行硬编码)
var multiple10 = [
'',
'one -- complicated!',
'twenty',
'thirty',
'forty',
'fifty',
]; //the list goes on
var multiple1 = [
'',
'one',
'two',
'three',
'four',
'five',
]; //the list goes on
var listOfNames = [multiple10, multiple1]; //the list goes on
String numberToString(int num) {
var s = num.toString();
var res = '';
for (int i = s.length - 1; i >= 0; i--) {
var c = int.parse(s[i]);
res = res + listOfNames[i][c] + " ";
}
return res;
}
void main() {
print(numberToString(55));
}
添加@Lesiak的答案,如果您想要Indian格式的字符串,这里是解决方案。
const oneToNine = [
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
];
const tenToNinteen = [
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
];
const dozens = [
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
];
String solution(int number) {
if (number == 0) {
return zero;
}
return generate(number).trim();
}
String generate(int number) {
if (number >= 10000000) {
return '${generate(number ~/ 10000000)} crore ${generate(number % 10000000)}';
}
else if (number >= 100000) {
return '${generate(number ~/ 100000)} lakh ${generate(number % 100000)}';
} else if (number >= 1000) {
return '${generate(number ~/ 1000)} thousand ${generate(number % 1000)}';
} else if (number >= 100) {
return '${generate(number ~/ 100)} hundred ${generate(number % 100)}';
}
return generate1To99(number);
}
String generate1To99(int number) {
if (number == 0) {
return "";
} else if (number <= 9) {
return oneToNine[number - 1];
} else if (number <= 19) {
return tenToNinteen[number % 10];
} else {
return '${dozens[number ~/ 10 - 1]} ${generate1To99(number % 10)}';
}
}
void main() {
print(solution(123456789123));
}