Skip to content

Instantly share code, notes, and snippets.

@opnchaudhary
Created February 6, 2013 11:18
Show Gist options
  • Save opnchaudhary/4721977 to your computer and use it in GitHub Desktop.
Save opnchaudhary/4721977 to your computer and use it in GitHub Desktop.
A codeigniter library to convert number into words
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Numbertowords {
function convert_number($number) {
if (($number < 0) || ($number > 999999999)) {
throw new Exception("Number is out of range");
}
$Gn = floor($number / 1000000);
/* Millions (giga) */
$number -= $Gn * 1000000;
$kn = floor($number / 1000);
/* Thousands (kilo) */
$number -= $kn * 1000;
$Hn = floor($number / 100);
/* Hundreds (hecto) */
$number -= $Hn * 100;
$Dn = floor($number / 10);
/* Tens (deca) */
$n = $number % 10;
/* Ones */
$res = "";
if ($Gn) {
$res .= $this->convert_number($Gn) . "Million";
}
if ($kn) {
$res .= (empty($res) ? "" : " ") .$this->convert_number($kn) . " Thousand";
}
if ($Hn) {
$res .= (empty($res) ? "" : " ") .$this->convert_number($Hn) . " 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 ($Dn || $n) {
if (!empty($res)) {
$res .= " and ";
}
if ($Dn < 2) {
$res .= $ones[$Dn * 10 + $n];
} else {
$res .= $tens[$Dn];
if ($n) {
$res .= "-" . $ones[$n];
}
}
}
if (empty($res)) {
$res = "zero";
}
return $res;
}
}
?>
@ishanrakitha
Copy link

it works nicely, but it won't read minus values. any fixes ?

@sabbirshawon
Copy link

how to add this to my views.

@vastrolorde
Copy link

how about decimal point?

@Raginilexic
Copy link

How to use this..??

@SachithR
Copy link

`<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Numbertowords {
function convert_number($number) {
$num=$number;
if (($number < 0) || ($number > 999999999)) {
throw new Exception("Number is out of range");
}
$Gn = floor($number / 1000000);
/* Millions (giga) /
$number -= $Gn * 1000000;
$kn = floor($number / 1000);
/
Thousands (kilo) /
$number -= $kn * 1000;
$Hn = floor($number / 100);
/
Hundreds (hecto) /
$number -= $Hn * 100;
$Dn = floor($number / 10);
/
Tens (deca) /
$n = $number % 10;
/
Ones */
$res = "";
if ($Gn) {
$res .= $this->convert_number($Gn) . "Million";
}
if ($kn) {
$res .= (empty($res) ? "" : " ") .$this->convert_number($kn) . " Thousand";
}
if ($Hn) {
$res .= (empty($res) ? "" : " ") .$this->convert_number($Hn) . " 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 ($Dn || $n) {
if (!empty($res)) {
$res .= " and ";
}
if ($Dn < 2) {
$res .= $ones[$Dn * 10 + $n];
} else {
$res .= $tens[$Dn];
if ($n) {
$res .= "-" . $ones[$n];
}
}
}
if (empty($res)) {
$res = "zero";
}

            $points = substr(number_format($num,2),-2,2);
            if($points > 0){
            $Dn = floor($points / 10);
	/* Tens (deca) */
	$n = $points % 10;
            /* Ones */
                
                if ($Dn || $n) {
		if (!empty($res)) {
			$res .= " and ";
		}
		if ($Dn < 2) {
			$res .= $ones[$Dn * 10 + $n];
		} else {
			$res .= $tens[$Dn];
			if ($n) {
				$res .= "-" . $ones[$n];
			}
		}
                    $res .= " Cents";
	}
            
            }
	return $res;
}

}
?>`

@SachithR
Copy link

In above comment I improved opnchaudhary's code to read points. Now it will give currency as "Cents" at the end.

@lugzdnas
Copy link

Hello. Code is very nice and working properly. how can i have a "billion" integrated in the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment