Hi ,
I want to convert numbers into words. Can anyone please help me to do that or how can i implement it in PHP codes pls tell me that.
bimalnayak 0 Newbie Poster
muralibobby2015 17 Posting Pro
Hi ,
I want to convert numbers into words. Can anyone please help me to do that or how can i implement it in PHP codes pls tell me that.
http://www.geekpedia.com/code28_Convert-a-number-from-digits-to-words.html
Edited by muralibobby2015 because: n/a
rajarajan2017
<?php
define("MAJOR", 'pounds');
define("MINOR", 'p');
class toWords {
var $pounds;
var $pence;
var $major;
var $minor;
var $words = '';
var $number;
var $magind;
var $units = array('','one','two','three','four','five','six','seven','eight','nine');
var $teens = array('ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
var $tens = array('','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety');
var $mag = array('','thousand','million','billion','trillion');
function toWords($amount, $major=MAJOR, $minor=MINOR) {
$this->major = $major;
$this->minor = $minor;
$this->number = number_format($amount,2);
list($this->pounds,$this->pence) = explode('.',$this->number);
$this->words = " $this->major $this->pence$this->minor";
if ($this->pounds==0)
$this->words = "Zero $this->words";
else {
$groups = explode(',',$this->pounds);
$groups = array_reverse($groups);
for ($this->magind=0; $this->magind<count($groups); $this->magind++) {
if (($this->magind==1)&&(strpos($this->words,'hundred') === false)&&($groups[0]!='000'))
$this->words = ' and ' . $this->words;
$this->words = $this->_build($groups[$this->magind]).$this->words;
}
}
}
function _build($n) {
$res = '';
$na = str_pad("$n",3,"0",STR_PAD_LEFT);
if ($na == '000') return '';
if ($na{0} != 0)
$res = ' '.$this->units[$na{0}] . ' hundred';
if (($na{1}=='0')&&($na{2}=='0'))
return $res . ' ' . $this->mag[$this->magind];
$res .= $res==''? '' : ' and';
$t = (int)$na{1}; $u = (int)$na{2};
switch ($t) {
case 0: $res .= ' ' . $this->units[$u]; break;
case 1: $res .= ' ' . $this->teens[$u]; break;
default:$res .= ' ' . $this->tens[$t] . ' ' . $this->units[$u] ; break;
}
$res .= ' ' . $this->mag[$this->magind];
return $res;
}
}
$amount = 550.50;
$obj = new toWords($amount);
echo $obj->words; // gives Twelve thousand three hundred forty five pounds 67p
echo $obj->number; // gives 12,345.67
$obj = new toWords( $amount , 'dollars', 'c');
echo $obj->words; // gives Twelve thousand three hundred forty five dollars 67c
echo $obj->number; // gives 12,345.67
?>
Execute it
muralibobby2015 17 Posting Pro
see this.
<?
function numtowords($num){
$ones = array(
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "fourteen",
15 => "fifteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen"
);
$tens = array(
2 => "twenty",
3 => "thirty",
4 => "forty",
5 => "fifty",
6 => "sixty",
7 => "seventy",
8 => "eighty",
9 => "ninety"
);
$hundreds = array(
"hundred",
"thousand",
"million",
"billion",
"trillion",
"quadrillion"
);
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr);
$rettxt = "";
foreach($whole_arr as $key => $i){
if($i < 20){
$rettxt .= $ones[$i];
}elseif($i < 100){
$rettxt .= $tens[substr($i,0,1)];
$rettxt .= " ".$ones[substr($i,1,1)];
}else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= " ".$hundreds[$key]." ";
}
}
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $ones[$decnum];
}elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
}
return $rettxt;
}
echo numtowords(2500);
?>
muralibobby2015 17 Posting Pro
see this.
<?
function numtowords($num){
$ones = array(
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "fourteen",
15 => "fifteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen"
);
$tens = array(
2 => "twenty",
3 => "thirty",
4 => "forty",
5 => "fifty",
6 => "sixty",
7 => "seventy",
8 => "eighty",
9 => "ninety"
);
$hundreds = array(
"hundred",
"thousand",
"million",
"billion",
"trillion",
"quadrillion"
);
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr);
$rettxt = "";
foreach($whole_arr as $key => $i){
if($i < 20){
$rettxt .= $ones[$i];
}elseif($i < 100){
$rettxt .= $tens[substr($i,0,1)];
$rettxt .= " ".$ones[substr($i,1,1)];
}else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= " ".$hundreds[$key]." ";
}
}
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $ones[$decnum];
}elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
}
return $rettxt;
}
echo numtowords(2500);
?>
pulpers 0 Newbie Poster
I want to convert numbers into words. Can anyone please help me to do that or how can i implement it in PHP codes pls tell me that.
i hope this will help!!
1.
<script type="text/javascript">
2.
var SingleDigits = new Array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
3.
var DoubleDigits = new Array("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
4.
5.
function DigitsToWords(Digits)
6.
{
7.
var Words = "";
8.
var St;
9.
if (Digits > 999)
10.
{
11.
return "The number exceeds 999.";
12.
}
13.
if (Digits == 0)
14.
{
15.
return SingleDigits[0];
16.
}
17.
for (var i = 9; i >= 1; i--)
18.
{
19.
if (Digits >= i * 100)
20.
{
21.
Words += SingleDigits[i];
22.
St = 1;
23.
Words += " hundred";
24.
if (Digits != i * 100) Words += " and ";
25.
{
26.
Digits -= i*100;
27.
}
28.
i=0;
29.
}
30.
}
31.
32.
for (var i = 9; i >= 2; i--)
33.
{
34.
if (Digits >= i * 10)
35.
{
36.
Words += (St?DoubleDigits[i-2].toLowerCase():DoubleDigits[i-2]);
37.
St = 1;
38.
if (Digits != i * 10) Words += "-";
39.
{
40.
Digits -= i*10;
41.
}
42.
i=0;
43.
}
44.
}
45.
46.
for (var i = 1; i < 20; i++)
47.
{
48.
if (Digits == i)
49.
{
50.
Words += (St?SingleDigits[i].toLowerCase():SingleDigits[i]);
51.
}
52.
}
53.
return Words;
54.
}
55.
56.
alert("Are you over " + DigitsToWords(21) + "?");
57.
</script>
Edited by Nick Evan because: Fixed formatting
diafol
Looks like bimalnayak has left the building! Save your replies until he comes back.
manzarr 1 Light Poster
try this function i think its useful to you
function adv_count_words($str)
{
$words = 0;
$str = eregi_replace(" +", " ", $str);
$array = explode(" ", $str);
for($i=0;$i < count($array);$i++)
{
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
$words++;
}
return $words;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.