Home > Software engineering >  Convert number with standard form into real number in Dart
Convert number with standard form into real number in Dart

Time:12-31

All i just want is to convert the integer 55 in a word form "fifty five" using Dart as a language. As other example is

int nums = 10294;
// convert it to 'one thousand two hundred ninety four'

CodePudding user response:

It looks like a problem that must have been solved before.

I would take the following steps:

  • try to define the problem in a precise way. For example: 'Convert number to words' is much better than 'Convert number with standard form into real number'. Real number has a precise meaning in maths.
  • google the solution: How to convert number to words in java
  • convert solution you like to dart
  • write tests
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));
}

CodePudding user response:

Essentially you need to have each numbers's literal equivalent, and they should be sorted by their range (1000s, 100s, 10s, 1s, etc). The following code produces 55, but you need to adapt it for 10294 and add some special cases (the numbers ten to nineteen probably have to be hard coded)

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));
}
  •  Tags:  
  • dart
  • Related