This program will ask the user to enter a number and then converts the given number into its words format in PHP. I also included a code that will check if the text field is empty and will inform the user to enter a number so that it can be convert into words.
If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360.
Thank you very much and Happy Productive Programming.
Sample Output of Our Program
Program Listing
<html>
<title> Number To Words Converter</title>
<style>
body {
font-size:20px;
font-family:arial;
color:blue;
}
</style>
<?php
error_reporting(0);
$values = $_POST['value1'];
$num = floatval($values);
if(isset($_POST['check'])) {
if ($values==" ") {
echo "<script>";
echo "alert('It Cannot Be Empty!!! Please enter a sentence.');";
echo "</script>";
$values=" ";
$results=" ";
}
}
$results .= "==== DISPLAY RESULT ==== ";
$results .= "<br><br>";
$results .= convert_number_to_words($num);
if(isset($_POST['clear'])) {
$values= " ";
$results= " ";
$num=" ";
}
function convert_number_to_words($number) {
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}
?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
<br> <h1 align="center">Number To Words Converter in PHP</h2>
<br>
<form action="" method="post">
Enter a Sentence : <input type="text" name="value1" value="<?php echo $values; ?>" autofocus size=10/>
<br><br>
<input type="submit" name="check" value="Convert to Words"
title="Click here to convert number into words."/>
<input type="submit" name="clear" value="Clear"
title="Click here to clear text box and values on the screen"/>
</form>
<br>
<?php
echo $results;
?>
</body>
</html>
DOWNLOAD SOURCE CODE HERE | ||