Numbers to Words conversion library for 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 e.g. 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.
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:
To use this javascript library, all you need to do is select all the code and save it in a file numbertoword.js and use it as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
?> |
<?phpNot only this, you can also use the following javascript library:
$a=$this->load->library('numbertowords');
$number=1234567890;
echo $this->numbertowords->convert_number($number);
?>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert numbers to words | |
// copyright 25th July 2006, by Stephen Chapman http://javascript.about.com | |
// permission to use this Javascript on your web page is granted | |
// provided that all of the code (including this copyright notice) is | |
// used exactly as shown (you can change the numbering system if you wish) | |
/* | |
Documentation: Usage | |
===================== | |
1. Import the javascript file: | |
<script src="numbertoword.js"></script> | |
2. Call the function toWords(number) passing a number to it: | |
<script> | |
var number=1000; | |
var Inwords=toWords(number); | |
<script> | |
*/ | |
// American Numbering System | |
var th = ['', 'thousand', 'million', 'billion', 'trillion']; | |
// uncomment this line for English Number System | |
// var th = ['','thousand','million', 'milliard','billion']; | |
var dg = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; | |
var tn = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']; | |
var tw = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']; | |
function toWords(s) { | |
s = s.toString(); | |
s = s.replace(/[\, ]/g, ''); | |
if (s != parseFloat(s)) | |
return 'not a number '; | |
var x = s.indexOf('.'); | |
if (x == -1) | |
x = s.length; | |
if (x > 15) | |
return 'too big'; | |
var n = s.split(''); | |
var str = ''; | |
var sk = 0; | |
for (var i = 0; i < x; i++) { | |
if ((x - i) % 3 == 2) { | |
if (n[i] == '1') { | |
str += tn[Number(n[i + 1])] + ' '; | |
i++; | |
sk = 1; | |
} else if (n[i] != 0) { | |
str += tw[n[i] - 2] + ' '; | |
sk = 1; | |
} | |
} else if (n[i] != 0) { | |
str += dg[n[i]] + ' '; | |
if ((x - i) % 3 == 0) | |
str += 'hundred '; | |
sk = 1; | |
} | |
if ((x - i) % 3 == 1) { | |
if (sk) | |
str += th[(x - i - 1) / 3] + ' '; | |
sk = 0; | |
} | |
} | |
if (x != s.length) { | |
var y = s.length; | |
str += 'point '; | |
for (var i = x + 1; i < y; i++) | |
str += dg[n[i]] + ' '; | |
} | |
return str.replace(/\s+/g, ' '); | |
} |
It should have solved your problem, right?
//Import the javascript file:<script src="numbertoword.js"></script>//Call the function toWords(number) passing a number to it: <script> var number=1000; var Inwords=toWords(number);<script>
hmm
ReplyDeleteAwesome . it worked perfectly for me .thumb up
ReplyDeletethanks alot.help appriciated
ReplyDeleteGreat.It helped.Thanks
ReplyDelete