Results 1 to 3 of 3

Thread: Number to Text

  1. #1
    Join Date
    Jun 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  3. #3
    Join Date
    Jun 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Number to Text

    You can do like this....

    In Header file-

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

    In Source file-
    Qt Code:
    1. QString MainWindow::NumberToWord(const unsigned int number)
    2. {
    3. const std::vector<QString> first14=
    4. {"Zero", "One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven",
    5. "Twelve", "Thirteen", "Fourteen" };
    6. const std::vector<QString> prefixes=
    7. {"Twen","Thir","For","Fif","Six","Seven","Eigh","Nine"};
    8.  
    9. if( number <= 14 )
    10. {
    11. return first14.at(number);
    12. }
    13. if( number < 20 )
    14. {
    15. return prefixes.at(number-12) + "teen";
    16. }
    17. if( number < 100 )
    18. {
    19. unsigned int remainder = number - (static_cast<int>(number/10)*10);
    20. return prefixes.at(number/10-2) + (0 != remainder ? "ty " + NumberToWord(remainder) : "ty");
    21. }
    22. if( number < 1000 )
    23. {
    24. unsigned int remainder = number - (static_cast<int>(number/100)*100);
    25. return first14.at(number/100) + (0 != remainder ? " Hundred " + NumberToWord(remainder) : " Hundred");
    26. }
    27. if( number < 1000000 )
    28. {
    29. unsigned int thousands = static_cast<int>(number/1000);
    30. unsigned int remainder = number - (thousands*1000);
    31. return NumberToWord(thousands) + (0 != remainder ? " Thousand " + NumberToWord(remainder) : " Thousand");
    32. }
    33. if( number < 1000000000 )
    34. {
    35. unsigned int millions = static_cast<int>(number/1000000);
    36. unsigned int remainder = number - (millions*1000000);
    37. return NumberToWord(millions) + (0 != remainder ? " Million " + NumberToWord(remainder) : " Million");
    38. }
    39.  
    40. throw std::out_of_range("NumberToWord() value too large");
    41. }
    To copy to clipboard, switch view to plain text mode 
    Its done!

    Now you can use like this-
    Qt Code:
    1. int number=ui->label_Number->text().toInt();
    2. QString word=NumberToWord(b);
    3.  
    4. 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 09:33. Reason: missing [code] tags

Similar Threads

  1. Replies: 1
    Last Post: 5th March 2012, 07:34
  2. Qt line number in text editor
    By Nyx in forum Qt Programming
    Replies: 6
    Last Post: 24th August 2010, 11:16
  3. Replies: 1
    Last Post: 22nd July 2010, 18:12
  4. How to read line number in a text file
    By grsandeep85 in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2009, 10:09
  5. count line number in text
    By Alina in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2006, 09:29

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.