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");
}
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!
int number=ui->label_Number->text().toInt();
ui->label_Words->setText(word);
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,
Bookmarks