QUERY_NU.C
LIB3/SECURE/SIMUL_EF/QUERY_NU.C · 1.4 KB
From the archive nm4dos.zip
Download the archive « Back to the archive contents
string number_as_string( int n );
string query_num( int n, int limit )
{
string ret;
int i;
if( limit && n > limit )
return "many"; /* this is a little pointless ... */
if( n < 0 )
return "many"; /* fo wraps... */
if( n > 99999 )
return "many"; /* sorry 'bout the hard limit */
ret = 0;
if( (i = n / 1000) )
{
n = n % 1000;
if( !n )
return number_as_string( i ) + " thousand";
ret = number_as_string( i ) + " thousand";
}
if( (i = n / 100) )
{
n = n % 100;
if( ret )
{
if( !n )
return ret + " and " + number_as_string( i ) + " hundred";
ret += ", " + number_as_string( i ) + " hundred";
}
else
{
if( !n )
return number_as_string( i ) + " hundred";
ret = number_as_string( i ) + " hundred";
}
}
if( ret )
return ret + " and " + number_as_string( n );
return number_as_string( n );
} /* query_num() */
string number_as_string( int n )
{
string ret;
if( !n )
return "zero";
if( n < 20 && n > 9 )
return({ "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" })[ n - 10 ];
ret = ({ "", "", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety" })[ n / 10 ];
if( (n = n % 10) && (ret != "") )
ret += " ";
return ret + ({ "", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine" })[ n ];
} /* number_as_string() */