📜 ⬆️ ⬇️

Number generation in words from any arbitrary number

Once I needed to write the generation of the amount in words for one project. The search for ready-made solutions led nowhere; as a result, a small C ++ class was born that generates a capital equivalent of a number. As a nice bonus, I added there to support rubles and dollars (this was required for that project). Under the cut a bit of theory and a link to the Git-repository.

Theory

Names of Numbers

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

Example:


Hundreds include tens and units , 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.
')
Female and male gender order names

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

There are such variants of numbers:

etc.

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


Example:



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

Class

The number generation mechanism works as follows:

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


This is how the C ++ class Propis was born . From a double value, the output is a string with an uppercase value and a currency of the number (if available) with an accuracy of hundredths. The generation of numbers up to 1 billion is implemented, if necessary, you can easily make support for billions and above. It depends only on the standard cmath and std :: string library.

Example of use 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 git repository with class source code is here . I would be glad if someone come in handy.

Source: https://habr.com/ru/post/158437/


All Articles