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
Printable View
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
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.
You can do like this....
In Header file-
public:
static QString NumberToWord(const unsigned int number);
In Source file-
Its done!Code:
{ 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"); }
Now you can use like this-
Thank you,Code:
int number=ui->label_Number->text().toInt(); ui->label_Words->setText(word);
Rahat Hossain