All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Convert Number To Words In PHP

Last Updated : Mar 11, 2024

Convert Number To Words In PHP

In this tutorial we will show you the solution of convert number to words in PHP, sometimes, a situation occurs when we have to convert a number to a word.

For example, in some mathematical operations, financial statements, and also in e-commerce systems.

Hence, there are many situations in which we have to convert numbers to words. So, let us understand how to do this in PHP.

Step By Step Guide On Convert Number To Words In PHP :-

Now, to do this we are going to show you an example with help of which we easily understand how to convert numbers to words in PHP.

<!DOCTYPE html>
<html>
<head>
   <title> how to convert numbers to words in PHP </title>
</head>
<body>
<h1>
 TalkersCode
</h1>
<h2>
 Convert numbers to words in PHP
</h2>
<?php
$object = new conversion_system();
$digits = 1245;
echo $object->convert_number($digits);
class conversion_system {
    function convert_number($number)
    {
        if (($number < 0) || ($number > 999999999))
        {
            throw new Exception("Number is not in our range");
        }
        $millions = floor($number / 1000000);
        $number -= $millions * 1000000;
        $thousands = floor($number / 1000);
        $number -= $thousands * 1000;
        $hundreds = floor($number / 100);
        $number -= $hundreds * 100;
        $tens = floor($number / 10);
        // Tens (deca)
        $n = $number % 10;
        // Ones
        $solution = "";
        if ($millions)
        {
            $solution .= $this->convert_number($millions) . "Million";
        }
        if ($thousands)
        {
            $solution .= (empty($solution) ? "" : " ") .$this->convert_number($thousands) . " Thousand";
        }
        if ($hundreds)
        {
            $solution .= (empty($solution) ? "" : " ") .$this->convert_number($hundreds) . " Hundred";
        }
        $ones = array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen", "Nineteen");
        $last = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eigthy", "Ninety");
        if ($last || $n) {
            if (!empty($solution))
            {
                $solution .= " and ";
            }
            if ($tens < 2)
            {
                $solution .= $ones[$tens * 10 + $n];
            } else {
                $solution .= $tens[$tens];
                if ($n)
                {
                    $solution .= "-" . $ones[$n];
                }
            }
        }
        if (empty($solution))
        {
            $solution = "zero";
        }
        return $solution;
    }
}
?>
</body>
</html>
  1. As, here we see that we that in above example we show you an example in which HTML and PHP codes are used.
  2. Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
  3. After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
  4. Now, next is our title tag which defines the title of the webpage. The tag which has its closing tag is known as a paired tag. So, this is again a paired tag.
  5. Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
  6. As here above see that here we create a class and a function also. Inside this function, we have done our calculation and returned the result at end of the function.
  7. Now, outside this class, we only create an object of the class and store the required numbers inside a variable, and pass this variable as a parameter. After this, we have to just use echo to display the result on the screen

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to convert numbers to words in PHP.

I hope this tutorial on convert number to words in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪