');
print('tens : '.$tens. '
');
print('units : '.$units. '
');
*/
// cascade trough hundreds. This will convert the hundreds part to
// their corresponding string in portuguese.
switch ($hundreds) {
case 1: $string_hundreds = "cento "; break; // Special case
case 2: $string_hundreds = "duzentos "; break;
case 3: $string_hundreds = "trezentos "; break;
case 4: $string_hundreds = "quatrocentos "; break;
case 5: $string_hundreds = "quinhentos "; break; // Special case
case 6: $string_hundreds = "seiscentos "; break;
case 7: $string_hundreds = "setecentos "; break; // Special case
case 8: $string_hundreds = "oitocentos "; break;
case 9: $string_hundreds = "novecentos "; break; // Special case
} // end switch hundreds
// casgade trough tens. This will convert the tens part to corresponding
// strings in portuguese. Note, however that the strings between 11 and 19
// are all special cases. Also 21-29 is a special case in portuguese.
switch ($tens) {
case 1: // Special case, depends on units for each conversion
switch($units){
case 1: $string_tens = "onze"; break; // Special case
case 2: $string_tens = "doze"; break; // Special case
case 3: $string_tens = "treze"; break; // Special case
case 4: $string_tens = "catorze"; break; // Special case
case 5: $string_tens = "quinze"; break; // Special case
case 6: $string_tens = "dezasseis"; break; // Special case
case 7: $string_tens = "dezassete"; break; // Special case
case 8: $string_tens = "dezoito"; break; // Special case
case 9: $string_tens = "dezanove"; break; // Special case
} break; // end switch special case tens/units
case 2: $string_tens = "vinte"; break; // Special case
case 3: $string_tens = "trinta"; break;
case 4: $string_tens = "quarenta"; break;
case 5: $string_tens = "cinquenta"; break;
case 6: $string_tens = "sessenta"; break;
case 7: $string_tens = "setenta"; break;
case 8: $string_tens = "oitenta"; break;
case 9: $string_tens = "noventa"; break;
} // end switch tens
// cascades trough units, This will convert the units part to corresponding
// strings in portuguese. Note however that a check is being made to see wether
// the special cases 11-19 were used. In that case, the whole conversion of
// individual units is ignored since it was already made in the tens cascade.
if ($tens == 1) {
$string_units=""; // empties the units check, since it has alredy been handled on the tens switch
} else {
switch ($units) {
case 1: $string_units = "um"; break;
case 2: $string_units = "dois"; break;
case 3: $string_units = "três"; break;
case 4: $string_units = "quatro"; break;
case 5: $string_units = "cinco"; break;
case 6: $string_units = "seis"; break;
case 7: $string_units = "sete"; break;
case 8: $string_units = "oito"; break;
case 9: $string_units = "nove"; break;
} // end switch units
} // end if-then-else
//final special cases. This conditions will handle the special cases which
//are not as general as the ones in the cascades. Basically four:
// when you've got 100, you dont' say 'ciento' you say 'cien'
// 'ciento' is used only for [101 >= number > 199]
if ($hundreds == 1 and $tens == 0 and $units == 0) {
$string_hundreds = "cem" ;
}
// when you've got 10, you don't say any of the 11-19 special
// cases.. just say 'dez'
if ($tens == 1 and $units ==0) {
$string_tens = "dez" ;
}
if ($tens == 2 and $units ==0) {
$string_tens = "vinte" ;
}
// for numbers >= 30, you don't use a single word such as veintiuno
// (twenty one), you must add 'y' (and), and use two words. v.gr 31
// 'treinta y uno' (thirty and one)
if ($tens >=2 and $units >=1) {
$string_tens = $string_tens." e ";
}
// this line gathers all the hundreds, tens and units into the final string
// and returns it as the function value.
$final_string = $string_hundreds.$string_tens.$string_units;
return $final_string ;
} //end of function string_literal_conversion()================================
/***************************
* MAIN PROGRAM STARTS HERE *
***************************/
// this echoes the input form to the browser
echo 'Please enter a number between 999,999,999 and 0';
echo '';
echo '';
echo 'Translated number to portuguese:
';
// handle some external special cases. Specially the millions, thousands
// and hundreds descriptors. Since the same rules apply to all number triads
// descriptions are handled outside the string conversion function, so it can
// be re used for each triad.
if ($number == 0) { // if amount = 0, then forget all about conversions,
$hundreds_final_string=" zero "; // amount is zero (cero). handle it externally, to
// function breakdown
} else {
$millions = whole_part($number, 1000000); // first, send the millions to the string
$number = mod($number, 1000000); // conversion function
if ($millions != 0) { // This condition handles the plural case
if ($millions == 1) { // if only 1, use 'millon' (million). if
$descriptor= " milhão, "; // > than 1, use 'millones' (millions) as
} else { // a descriptor for this triad.
$descriptor = " milhões, ";
}
} else {
$descriptor = " "; // if 0 million then use no descriptor.
}
$millions_final_string = string_literal_conversion($millions).$descriptor;
$thousands = whole_part($number, 1000); // now, send the thousands to the string
$number = mod($number, 1000); // conversion function.
if ($thousands != 0) { // This condition eliminates the descriptor
$descriptor = " mil, "; // if there are no thousands on the amount
} else {
$descriptor= " ";
}
$thousands_final_string =string_literal_conversion($thousands) .$descriptor;
// this will handle numbers between 1 and 999 which
// need no descriptor whatsoever.
$hundreds = $number;
$hundreds_final_string = string_literal_conversion($hundreds) ;
} //end if ($number ==0)
//finally, print the output.
print "".$millions_final_string.$thousands_final_string.$hundreds_final_string ."" ;
// that's it.
?>