1. Beginner Join Date
Jun 2012
Posts
1
Qt products Platforms  ## Number to Text

Hi guys in looking for a function that help me with this problem, i have a number (int) and i need to pass it to text i mean like this 100 -> ONE HUNDRED so if anyone can help ill appreciate it
thanks
Markov  Reply With Quote

2. Guru           Join Date
Mar 2009
Location
Brisbane, Australia
Posts
7,734
Thanks
13
Thanked 1,610 Times in 1,537 Posts
Qt products  Platforms  Wiki edits
17

## Re: Number to Text

There is nothing in Qt to do that for you. Boost Locale might be able to help but it will be subtly incorrect even for English, e.g. 103 => "One hundred three" in some places versus "One hundred and three" in my part of the English-speaking world.

A naive approach for English might start like:
• Break the number in groups of three digits, e.g. 123456 is two groups 123 and 456
• Take the first digit in each group and convert it to a word and append "hundred" e.g. one hundred, 4 hundred
• If the second digit is greater than 1 convert it to a word, e.g. "twenty" or "fifty", and append the last digit converted, e.g. "twenty-three" or "fifty-six"
• If the second digit is 1 then combine it with the last digit and lookup e.g. "eleven", "seventeen" etc
• If the second digit is 0 then simply convert the last digit
• Once you have all the groups converted put them together with the appropriate multiplier words, e.g. "thousand", "million", "billion".

For French the rules would be different because of a tendency to count in 20s, 84 => quatre-vingt-quatre. I'm sure that for many languages and regions there'd be new rules.  Reply With Quote

3. Beginner Join Date
Jun 2016
Posts
1
Qt products Platforms ## Re: Number to Text

You can do like this....

public:
static QString NumberToWord(const unsigned int number);

In Source file-
Qt Code:
`QString MainWindow::NumberToWord(const unsigned int number){    const std::vector<QString> first14=    {"Zero", "One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven",     "Twelve", "Thirteen", "Fourteen" };    const std::vector<QString> prefixes=    {"Twen","Thir","For","Fif","Six","Seven","Eigh","Nine"};     if( number <= 14 )    {        return first14.at(number);    }    if( number < 20 )    {        return prefixes.at(number-12) + "teen";    }    if( number < 100 )    {        unsigned int remainder = number - (static_cast<int>(number/10)*10);        return prefixes.at(number/10-2) + (0 != remainder ? "ty " + NumberToWord(remainder) : "ty");    }    if( number < 1000 )    {        unsigned int remainder = number - (static_cast<int>(number/100)*100);        return first14.at(number/100) + (0 != remainder ? " Hundred " + NumberToWord(remainder) : " Hundred");    }    if( number < 1000000 )    {        unsigned int thousands = static_cast<int>(number/1000);        unsigned int remainder = number - (thousands*1000);        return NumberToWord(thousands) + (0 != remainder ? " Thousand " + NumberToWord(remainder) : " Thousand");    }    if( number < 1000000000 )    {        unsigned int millions = static_cast<int>(number/1000000);        unsigned int remainder = number - (millions*1000000);        return NumberToWord(millions) + (0 != remainder ? " Million " + NumberToWord(remainder) : " Million");    }     throw std::out_of_range("NumberToWord() value too large");}`
To copy to clipboard, switch view to plain text mode
Its done!

Now you can use like this-
Qt Code:
`int number=ui->label_Number->text().toInt();QString word=NumberToWord(b); ui->label_Words->setText(word);`
To copy to clipboard, switch view to plain text mode
Thank you,
Rahat Hossain
Last edited by anda_skoa; 23rd February 2017 at 08:33. Reason: missing [code] tags  Reply With Quote

number, number formatting, numbering, string, text 