convert number to words in php – how to convert number to words in php?

how to convert number to words in php?

In this post we will show you convert number to words in php, hear for convert number to words in php we will give you demo and example for implement.

If you are writing an application that deals with financial stuffs, then most probably you will come to a situation where you would have to convert numerical values to words Example: to write the amounts in words.

Create file Name with numbertowordconvertsconver.php

convert number to words in php

[php]
class numbertowordconvertsconver {
function convert_number($number)
{
if (($number < 0) || ($number > 999999999))
{
throw new Exception(“Number is out of range”);
}
$giga = floor($number / 1000000);
// Millions (giga)
$number -= $giga * 1000000;
$kilo = floor($number / 1000);
// Thousands (kilo)
$number -= $kilo * 1000;
$hecto = floor($number / 100);
// Hundreds (hecto)
$number -= $hecto * 100;
$deca = floor($number / 10);
// Tens (deca)
$n = $number % 10;
// Ones
$result = “”;
if ($giga)
{
$result .= $this->convert_number($giga) . “Million”;
}
if ($kilo)
{
$result .= (empty($result) ? “” : ” “) .$this->convert_number($kilo) . ” Thousand”;
}
if ($hecto)
{
$result .= (empty($result) ? “” : ” “) .$this->convert_number($hecto) . ” Hundred”;
}
$ones = array(“”, “One”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”, “Ten”, “Eleven”, “Twelve”, “Thirteen”, “Fourteen”, “Fifteen”, “Sixteen”, “Seventeen”, “Eightteen”, “Nineteen”);
$tens = array(“”, “”, “Twenty”, “Thirty”, “Fourty”, “Fifty”, “Sixty”, “Seventy”, “Eigthy”, “Ninety”);
if ($deca || $n) {
if (!empty($result))
{
$result .= ” and “;
}
if ($deca < 2) { $result .= $ones[$deca * 10 + $n]; } else { $result .= $tens[$deca]; if ($n) { $result .= “-” . $ones[$n]; } } } if (empty($result)) { $result = “zero”; } return $result; } } ?>
[/php]

This library can be used in your CodeIgniter project. And the usage is very simple. Just import the library and call the function as follows:

See also :  include_once in php - PHP include and include_once

[php]
?>
[/php]

Hope this code and post will helped you for implement convert number to words in php”. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve itsolutionstuck.

Leave a Comment