# Board index » cppbuilder » number to character string

## number to character string

2004-12-07 08:14:29 AM
cppbuilder102
Hi,
Does anyone know if there is a library or code somewhere that converts number to character string?
For example:
1 = ONE
2 = TWO
10 = TEN
17 = SEVEN{*word*249}
101 = ONE HUNRED AND ONE
1011 = ONE THOUSAND AND ELEVEN
ect...

## Re:number to character string

That is a common homework problem. Try searching on google
for it. You'll probably find hundreds of solutions.
. Ed
##### Quote
BJ wrote in message
news:41b4f5e5\$ XXXX@XXXXX.COM ...

Does anyone know if there is a library or code
somewhere that converts number to character string?

For example:

1 = ONE
2 = TWO
10 = TEN
17 = SEVEN{*word*249}
101 = ONE HUNRED AND ONE
1011 = ONE THOUSAND AND ELEVEN

ect...

## Re:number to character string

"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
##### Quote
That is a common homework problem. Try searching on google
for it. You'll probably find hundreds of solutions.

. Ed
Thanks Ed.

{smallsort}

## Re:number to character string

"BJ" < XXXX@XXXXX.COM >wrote:
##### Quote

wrap them for you but you need to enter a hard return at the
end of each line.
##### Quote
Does anyone know if there is a library or code somewhere that converts number to character string?
The following is part of an old check-writing routine that I
reduced to a working example:
AnsiString OnesText[] = { "Dummy", "One", "Two", "Three",
"Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thir{*word*249}", "Four{*word*249}",
"Fif{*word*249}"
};
AnsiString TensText[] = { "Dummy", "Ten", "Twenty", "Thirty",
"Fourty", "Fifity", "Sixty",
"Seventy", "Eighty", "Ninety"
};
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double Amount = 11223344.99;
ShowMessage( CheckAmountToString( Amount ) );
}
//-------------------------------------------------------------
AnsiString __fastcall TForm1::CheckAmountToString( double Amount )
{
int Number = Amount;
int Cents = (Amount - Number) * 100;
AnsiString s = "";
for( int Factor = 1000000000; Factor>0; Factor /= 1000 )
{
if( Number / Factor )
{
int Hundreds = (Number / Factor) / 100;
int Tens = ((Number / Factor) % 100) / 10;
int Ones = (Number / Factor) % 10;
if( !s.IsEmpty() ) s += " ";
if( Hundreds )
{
s += OnesText[ Hundreds ] + " Hundred";
if( Tens || Ones ) s += " " + FormatTensAndOnes( Tens, Ones );
}
else s += FormatTensAndOnes( Tens, Ones );
switch( Factor )
{
case 1000: s += " Thousand"; break;
case 1000000: s += " Million"; break;
case 1000000000: s += " Billion";
}
}
Number %= Factor;
}
s += " And ";
if( Cents ) s += IntToStr( Cents );
else s+= "No";
s += "/100";
return s;
}
//-------------------------------------------------------------
AnsiString __fastcall TForm1::FormatTensAndOnes( int Tens, int Ones )
{
AnsiString s = "";
if( Tens )
{
if( Ones )
{
if( Tens>1 ) s = TensText[ Tens ] + " " + OnesText[ Ones ];
else if( Ones>5 ) s = OnesText[ Ones ] + "{*word*249}";
else s = OnesText[ Ones + 10 ];
}
else s = TensText[ Tens ];
}
else if( Ones ) s = OnesText[ Ones ];
return s;
}
//-------------------------------------------------------------
##### Quote
[...] 1011 = ONE THOUSAND AND ELEVEN
FIW : 'AND' is used to denote the decimal place and should not
be used as illustrated by you.
~ JD