Hello guys! I got new program for you and it’s a bit tricky to solve this one for me. It requires a really deep analysis for me to come up with a solution. So I created a program that converts a number into words. I did use arrays to store the values from one to nine, another array for eleven to nineteen and another array to store values from ten, twenty…to ninety and an if conditional statement to decide what to execute.
The program allows you to enter a number from 1 to 9999 only. It is not yet a full blown program and it still needs improvement. Perhaps later or maybe you can do it for me. ^_^
So here is the PHP Code that converts numbers from one (1) to nine thousand nine hundred ninety nine (9999).
PHP Code that converts numbers to words
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="number" name="num">
<button type="submit">Convert</button>
</form>
</body>
</html>
<?php
error_reporting(0);
if (isset($_POST['num']))
{
$num=$_POST['num'];
$len=strlen($num);
$ones=array("one","two","three","four","five","six","seven","eight","nine");
$ex=array("eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen");
$tens=array("ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety");
if($len==1)
{
$fir=substr($num,0,1);
}
if($len==2)
{
$fir=substr($num,1,2);
$sec=substr($num,0,1);
}
if ($len==3){
$fir=substr($num,2,3);
$sec=substr($num,1,1);
$thi=substr($num,0,1);
$thi=$thi-1;
echo $ones[$thi]." "."hundred ";
}
if ($len==4){
$fir=substr($num,3,4);
$sec=substr($num,2,1);
$thi=substr($num,1,1);
$for=substr($num,0,1);
$for=$for-1;
echo $ones[$for]." "."thousand ";
$thi=$thi-1;
echo $ones[$thi]." "."hundred ";
}
if (($sec==1) and ($sec<>0))
{
$fir=$fir-1;
echo $ex[$fir]." ";
}
else
{
$sec=$sec-1;
echo $tens[$sec]." ";
$fir=$fir-1;
echo $ones[$fir];
}
}
?>
No comments:
Post a Comment