loading...

convert number to words in codeigniter PHP

pakainfocom profile image pakainfo ・2 min read

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

convert number to words in codeigniter

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. And if the situation has come then you are at the right place, here is a CodeIgniter library that will help you with what you are really in need of.

Create library Name with numbertowordconvertsconver.php


if (!defined('BASEPATH'))
  exit('No direct script access allowed');
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;
    }
}

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:


$val = $this->load->library('numbertowordconvertsconver');
$number = 1234567890;
echo $this->numbertowordconvertsconver->convert_number($number);

Also You can learn more way about convert number to words in codeigniter

Posted on by:

pakainfocom profile

pakainfo

@pakainfocom

My name is Angular King. I'm a full-stack developer, entrepreneur and owner of Pakainfo. I live in India and I love to write

Discussion

pic
Editor guide