在 Dart 中将标准形式的数字转换为实数

问题描述 投票:0回答:3

我只想使用 Dart 作为语言将整数

55
转换为单词形式
"fifty-five"
。另一个例子是

int nums = 10294;
// convert it to 'ten thousand two hundred ninety four'
dart
3个回答
4
投票

看起来这个问题之前一定已经解决了。

我会采取以下步骤:

  • 尝试以精确的方式定义问题。例如:“将数字转换为单词”比“将标准形式的数字转换为实数”要好得多。 实数在数学中具有精确的含义。
  • 谷歌解决方案:如何在java中将数字转换为单词
  • 将您喜欢的解决方案转换为飞镖
  • 编写测试
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));
}

0
投票

本质上,您需要每个数字的字面等值,并且它们应该按其范围(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));
}

0
投票

添加@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));
}
© www.soinside.com 2019 - 2024. All rights reserved.