Automatically Display (or) Convert a Number into Word(String) Using PHP

By | September 13, 2012

Here we have to discuss about the thing how to convert automatically a number as string(word) using PHP. We already discuss the things in  conversion of current webpage as MS-Word document and PDF file. Similar to this conversion we going to convert a number as word(string). It  is also used in the application projects. Most of the billing system required automatically generate the total amount in word after entering the total amount. Similar to this most of webpage need to convert the entering number into word(string).

There is three cases in conversion of word. We able to convert the number as word(string) in following cases.

  • Upper Case
  • Lower Case
  • Camel Case

syntax

[code type= php]

strtoupper(convertNumber($num))

strtolower(convertNumber($num))

ucwords(convertNumber($num))

[/code]

Let’s see the simple example”

Automatically display(or) Convert a number into string using php

[code type = php]

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Example for Automatically Display (or) Convert a Number into String Using PHP</title>
<meta name=”description” content=”Example of converting number to word with PHP”>

<style type=”text/css”>

.well {
width: 500px;
}

.well h2 {
margin-bottom: 20px;
}

.well p {
color: #bf0000;
}

button[type=’submit’] {
display: block;
}
.container
{
background:#99B1F2;
width:400px;
height:200px;
text-align:justify;
padding:10px;
}

</style>

</head>
<body>
<center>
<div class=”container”>
<h2>Enter a number to convert it in words</h2>
<form class=”well” method=”POST” action=” ” >
<label>Enter a Number:</label>
<input type=”text” class=”span3″ name=”num” placeholder=”Enter a number”></br></br>
<label>Output Format:</label>
<select name=”cases”>
<option value=”uc”>All upper case</option>
<option value=”lc”>All lower case</option>
<option value=”cc”>Camel case</option>
</select>
<button type=”submit” name=”submit” >Submit</button>

</form>

</div>
</center>

</body>
</html>

<?php
/*
Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text
You can use this freely and modify it however you want.
*/

/*
w3resource team have modified it to make it nicer coverting cases.
*/

$num = $_POST[‘num’];

if ($_POST[‘cases’] == “uc”)
{
echo “<center><div class=’well’><h2>Your output</h2><p>”.strtoupper(convertNumber($num)).”</p></div></center>”;
}

if ($_POST[‘cases’] == “lc”)
{
echo “<center><div class=’well’><h2>Your output</h2><p>”.strtolower(convertNumber($num)).”</p></div></center>”;
}

if ($_POST[‘cases’] == “cc”)
{
echo “<center><div class=’well’><h2>Your output</h2><p>”.ucwords(convertNumber($num)).”</p></div></center>”;
}

function convertNumber($num)
{
list($num, $dec) = explode(“.”, $num);

$output = “”;

if($num{0} == “-“)
{
$output = “negative “;
$num = ltrim($num, “-“);
}
else if($num{0} == “+”)
{
$output = “positive “;
$num = ltrim($num, “+”);
}

if($num{0} == “0”)
{
$output .= “zero”;
}
else
{
$num = str_pad($num, 36, “0”, STR_PAD_LEFT);
$group = rtrim(chunk_split($num, 3, ” “), ” “);
$groups = explode(” “, $group);

$groups2 = array();
foreach($groups as $g) $groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});

for($z = 0; $z < count($groups2); $z++)
{
if($groups2[$z] != “”)
{
$output .= $groups2[$z].convertGroup(11 – $z).($z < 11 && !array_search(”, array_slice($groups2, $z + 1, -1))
&& $groups2[11] != ” && $groups[11]{0} == ‘0’ ? ” and ” : “, “);
}
}

$output = rtrim($output, “, “);
}

if($dec > 0)
{
$output .= ” point”;
for($i = 0; $i < strlen($dec); $i++) $output .= ” “.convertDigit($dec{$i});
}

return $output;
}

function convertGroup($index)
{
switch($index)
{
case 11: return ” decillion”;
case 10: return ” nonillion”;
case 9: return ” octillion”;
case 8: return ” septillion”;
case 7: return ” sextillion”;
case 6: return ” quintrillion”;
case 5: return ” quadrillion”;
case 4: return ” trillion”;
case 3: return ” billion”;
case 2: return ” million”;
case 1: return ” thousand”;
case 0: return “”;
}
}

function convertThreeDigit($dig1, $dig2, $dig3)
{
$output = “”;

if($dig1 == “0” && $dig2 == “0” && $dig3 == “0”) return “”;

if($dig1 != “0”)
{
$output .= convertDigit($dig1).” hundred”;
if($dig2 != “0” || $dig3 != “0”) $output .= ” and “;
}

if($dig2 != “0”) $output .= convertTwoDigit($dig2, $dig3);
else if($dig3 != “0”) $output .= convertDigit($dig3);

return $output;
}

function convertTwoDigit($dig1, $dig2)
{
if($dig2 == “0”)
{
switch($dig1)
{
case “1”: return “ten”;
case “2”: return “twenty”;
case “3”: return “thirty”;
case “4”: return “forty”;
case “5”: return “fifty”;
case “6”: return “sixty”;
case “7”: return “seventy”;
case “8”: return “eighty”;
case “9”: return “ninety”;
}
}
else if($dig1 == “1”)
{
switch($dig2)
{
case “1”: return “eleven”;
case “2”: return “twelve”;
case “3”: return “thirteen”;
case “4”: return “fourteen”;
case “5”: return “fifteen”;
case “6”: return “sixteen”;
case “7”: return “seventeen”;
case “8”: return “eighteen”;
case “9”: return “nineteen”;
}
}
else
{
$temp = convertDigit($dig2);
switch($dig1)
{
case “2”: return “twenty-$temp”;
case “3”: return “thirty-$temp”;
case “4”: return “forty-$temp”;
case “5”: return “fifty-$temp”;
case “6”: return “sixty-$temp”;
case “7”: return “seventy-$temp”;
case “8”: return “eighty-$temp”;
case “9”: return “ninety-$temp”;
}
}
}

function convertDigit($digit)
{
switch($digit)
{
case “0”: return “zero”;
case “1”: return “one”;
case “2”: return “two”;
case “3”: return “three”;
case “4”: return “four”;
case “5”: return “five”;
case “6”: return “six”;
case “7”: return “seven”;
case “8”: return “eight”;
case “9”: return “nine”;
}
}

?>

[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *

cuirie-propitiable