Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting numbers to text, C++

For an input number say 232, I wanted to be able to write out the number in text form: two hundred thirty two. I have an array which holds these numbers

Array[0] = 2, Array[1] = 3, Array[2] = 2.

I have written a

switch statement

which sees the number and prints it text, example two hundred three two. I don't know how transform that "three" into "thirty" dynamically. Suppose I have more numbers to spell, like 452,232.

like image 233
Chen Li Avatar asked Dec 07 '12 14:12

Chen Li


People also ask

How do I convert a number to a string in C?

sprintf() Function to Convert an Integer to a String in C As its name suggests, it prints any value into a string. This function gives an easy way to convert an integer to a string. It works the same as the printf() function, but it does not print a value directly on the console but returns a formatted string.

What does Strtol mean in C?

In the C Programming Language, the strtol function converts a string to a long integer. The strtol function skips all white-space characters at the beginning of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first character that isn't a number.

How do you turn a number into a character?

char a = Character. forDigit(num1, 10); We have used the forDigit() method converts the specified int value into char value. Here, 10 and 16 are radix values for decimal and hexadecimal numbers respectively.


2 Answers

You can't handle digits independently, it's that simple.

For example, the text for 21 is the concatenation of "twenty" and "one", but the text for 11 is not the concatenation of "ten" and "one".

Also, "1001" doesn't become "one thousand zero hundred zero one".

You can use function calls to keep logic complexity down, but you're going to need logic to look at multiple digits at once.

like image 104
Ben Voigt Avatar answered Oct 11 '22 13:10

Ben Voigt


Check out this implementation over at wikipedia. It is probably what you want

Copied directly from wikipedia, should the link become broken.
Do see the link first, if an improved solution would be written

#include <string>
#include <iostream>
using std::string;

const char* smallNumbers[] = {
  "zero", "one", "two", "three", "four", "five",
  "six", "seven", "eight", "nine", "ten",
  "eleven", "twelve", "thirteen", "fourteen", "fifteen",
  "sixteen", "seventeen", "eighteen", "nineteen"
};

string spellHundreds(unsigned n) {
  string res;
  if (n > 99) {
    res = smallNumbers[n/100];
    res += " hundred";
    n %= 100;
    if (n) res += " and ";
  }
  if (n >= 20) {
    static const char* Decades[] = {
      "", "", "twenty", "thirty", "forty",
      "fifty", "sixty", "seventy", "eighty", "ninety"
    };
    res += Decades[n/10];
    n %= 10;
    if (n) res += "-";
  }
  if (n < 20 && n > 0)
    res += smallNumbers[n];
  return res;
}


const char* thousandPowers[] = {
  " billion", " million",  " thousand", "" };

typedef unsigned long Spellable;

string spell(Spellable n) {
  if (n < 20) return smallNumbers[n];
  string res;
  const char** pScaleName = thousandPowers;
  Spellable scaleFactor = 1000000000;   // 1 billion
  while (scaleFactor > 0) {
    if (n >= scaleFactor) {
      Spellable h = n / scaleFactor;
      res += spellHundreds(h) + *pScaleName;
      n %= scaleFactor;
      if (n) res += ", ";
    }
    scaleFactor /= 1000;
    ++pScaleName;
  }
  return res;
}

int main() {
#define SPELL_IT(x) std::cout << #x " " << spell(x) << std::endl;
  SPELL_IT(      99);
  SPELL_IT(     300);
  SPELL_IT(     310);
  SPELL_IT(    1501);
  SPELL_IT(   12609);
  SPELL_IT(  512609);
  SPELL_IT(43112609);
  SPELL_IT(1234567890);
  return 0;
}
like image 34
default Avatar answered Oct 11 '22 13:10

default