Generate a number in words from any arbitrary number

Once I needed to write the amount generation in words for one project. The search for ready-made solutions did not lead to anything, as a result, a small C ++ class was born that generated the capital equivalent of a number. As a pleasant bonus, I added support for rubles and dollars there (this was required for that project). Under cat there is a bit of theory and a link to the Git repository.

Theory

Number names

In Russian, numbers from 1 to 9 (so-called units ) and from 10 to 20 (second row of units) have individual names. After 20, individual names have dozens , and a number is formed by adding dozens of category numbers from the category of units to the name.

Example:
  • 5 - five
  • 17 - seventeen
  • 23 - twenty three
  • 48 - forty eight


Hundreds include tens and ones , and thousands contain all of the above names. And so, as the order of the number increases, that is, each new order includes all the previous ones.

Feminine and masculine gender order names

There is the following feature. When forming a number less than 100, there are no problems, but when forming the capital equivalent of large sums, you get words that indicate orders of hundreds, tens and units, namely thousands, millions, etc.

There are such options for numbers:
  • one thousand
  • one million
  • two million
  • two thousand
  • five thousand

etc.

The rules are as follows:
  1. For the names of orders of the feminine, it is necessary to modify 1 and 2 ( one , two );
  2. For both genera, the endings of order names for number 1, groups from 2 to 4 and further to 20 (inclusive of the second row of units from 11 to 19) are inclined differently.


Example:
  • 1 (one) thousand a
  • 2-3-4 thousand and
  • 5-19 thousand

  • 1 million
  • 2-3-4 million a
  • 5-20 million s


Knowing these features, it is possible to compose the logic of the capital number generator.

Class

The number generation mechanism works as follows:

  1. Having received a number at the input, we determine the category of the number (millions, thousands or less);
  2. For each order except generate hundreds, tens and units, appending the name of the order at the end.


And so the C ++ class Propis was born . From the double value at the output, a string is obtained with an uppercase value and the currency of the number (if any) with an accuracy of hundredths. Implemented the generation of numbers up to 1 billion, if necessary, you can easily make support for billions and above. Depends only on the standard cmath and std :: string libraries .

Example usage in C ++ code:
Propis *propis = new Propis;
double value = 345.12;
// Задаем число для генерации и тип валюты
std::string resultString = propis->conv(value, Propis::Dollar);
// "триста сорок пять долларов, двенадцать центов"


An example of use in an iOS program:
- (IBAction)valueChanged:(UITextField *)sender
{
    Propis propis;
    double value = [sender.text doubleValue];
    std::string stdResultString = propis.conv(value, Propis::NoCurrency);
    NSString *resultString = [NSString stringWithUTF8String:stdResultString.c_str()];
    self.resultView.text = resultString;
}


The class source code repository is here . I would be glad if someone comes in handy.

Also popular now: