Convert Number to Words in Indian currency format with paise value
Convert Currency Number to Word Format Using PHP
<?php
/**
* Created by PhpStorm.
* User: sakthikarthi
* Date: 9/22/14
* Time: 11:26 AM
* Converting Currency Numbers to words currency format
*/
$number = 190908100.25;
$no = floor($number);
$point = round($number - $no, 2) * 100;
$hundred = null;
$digits_1 = strlen($no);
$i = 0;
$str = array();
$words = array('0' => '', '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', '20' => 'twenty',
'30' => 'thirty', '40' => 'forty', '50' => 'fifty',
'60' => 'sixty', '70' => 'seventy',
'80' => 'eighty', '90' => 'ninety');
$digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
while ($i < $digits_1) {
$divider = ($i == 2) ? 10 : 100;
$number = floor($no % $divider);
$no = floor($no / $divider);
$i += ($divider == 10) ? 1 : 2;
if ($number) {
$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
$hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
$str [] = ($number < 21) ? $words[$number] .
" " . $digits[$counter] . $plural . " " . $hundred
:
$words[floor($number / 10) * 10]
. " " . $words[$number % 10] . " "
. $digits[$counter] . $plural . " " . $hundred;
} else $str[] = null;
}
$str = array_reverse($str);
$result = implode('', $str);
$points = ($point) ?
"." . $words[$point / 10] . " " .
$words[$point = $point % 10] : '';
echo $result . "Rupees " . $points . " Paise";
?>
Output
nineteen crores nine lakh eight thousand one hundred Rupees . two five Paise
Now i have added Simplified Method as follows:function getIndianCurrency(float $number)
{
$decimal = round($number - ($no = floor($number)), 2) * 100;
$hundred = null;
$digits_length = strlen($no);
$i = 0;
$str = array();
$words = array(0 => '', 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', 20 => 'twenty', 30 => 'thirty',
40 => 'forty', 50 => 'fifty', 60 => 'sixty',
70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
$digits = array('', 'hundred','thousand','lakh', 'crore');
while( $i < $digits_length ) {
$divider = ($i == 2) ? 10 : 100;
$number = floor($no % $divider);
$no = floor($no / $divider);
$i += $divider == 10 ? 1 : 2;
if ($number) {
$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
$hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
$str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
} else $str[] = null;
}
$Rupees = implode('', array_reverse($str));
$paise = ($decimal > 0) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
}
method calling :
echo getIndianCurrency(79855995.19);
output
seven crore ninety eight lakhs fifty five thousands nine hundred and ninety five Rupees .one nine Paise
Express Amount In Words with Decimal Value
There's no defined spell-out formatter for currency, and while you could probably write one I think that that might be a bit of overkill.
What you could do instead is split the dollars as cents into separate values, spell those out, and combine them.
First and foremost though, you do not want to store or compute currencies with floating point representations. I was going to save this point for last, but I couldn't even get through the initial steps before floating point errors crept in.
$v = 2903.04;
$d = (int)$v; // casting to int discards decimal portion
$c = (int)(($v - $d) * 100);
var_dump($v, $d, ($v - $d) * 100, $c);
Output:
float(2903.04)
int(2903)
float(3.9999999999964)
int(3)
Use something like moneyphp/money which stores monetary values as integer amounts of base currency units. [eg: $2903.04 == 290304] This avoids errors like the above, as well as messy kludges to do with rounding. Additionally, money libraries will implement safe mathematical operations to do operations like dividing $1.00 among 3 recipients without splitting or losing pennies.
Instead, let's write the code like:
$a = 290304; // full amount in cents
$c = $a % 100; // cent remainder
$d = ($a - $c) / 100; // dollars
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
var_dump(
$a, $d, $c,
sprintf("%s dollars and %s cents", $f->format($d), $f->format($c))
);
Output:
int(290304)
int(2903)
int(4)
string(54) "two thousand nine hundred three dollars and four cents"
Is there an easy way to convert a number to a word in PHP?
I found some (2007/2008) source-code online and as it is copyright but I can use it freely and modify it however I want, so I place it here and re-license under CC-Wiki:
<?php
/**
* English Number Converter - Collection of PHP functions to convert a number
* into English text.
*
* This exact code is licensed under CC-Wiki on Stackoverflow.
* http://creativecommons.org/licenses/by-sa/3.0/
*
* @link http://stackoverflow.com/q/277569/367456
* @question Is there an easy way to convert a number to a word in PHP?
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text
* You can use this freely and modify it however you want.
*/
function convertNumber($number)
{
list($integer, $fraction) = explode(".", (string) $number);
$output = "";
if ($integer{0} == "-")
{
$output = "negative ";
$integer = ltrim($integer, "-");
}
else if ($integer{0} == "+")
{
$output = "positive ";
$integer = ltrim($integer, "+");
}
if ($integer{0} == "0")
{
$output .= "zero";
}
else
{
$integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
$group = rtrim(chunk_split($integer, 3, " "), " ");
$groups = explode(" ", $group);
$groups2 = array();
foreach ($groups as $g)
{
$groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
}
for ($z = 0; $z < count($groups2); $z++)
{
if ($groups2[$z] != "")
{
$output .= $groups2[$z] . convertGroup(11 - $z) . (
$z < 11
&& !array_search('', array_slice($groups2, $z + 1, -1))
&& $groups2[11] != ''
&& $groups[11]{0} == '0'
? " and "
: ", "
);
}
}
$output = rtrim($output, ", ");
}
if ($fraction > 0)
{
$output .= " point";
for ($i = 0; $i < strlen($fraction); $i++)
{
$output .= " " . convertDigit($fraction{$i});
}
}
return $output;
}
function convertGroup($index)
{
switch ($index)
{
case 11:
return " decillion";
case 10:
return " nonillion";
case 9:
return " octillion";
case 8:
return " septillion";
case 7:
return " sextillion";
case 6:
return " quintrillion";
case 5:
return " quadrillion";
case 4:
return " trillion";
case 3:
return " billion";
case 2:
return " million";
case 1:
return " thousand";
case 0:
return "";
}
}
function convertThreeDigit($digit1, $digit2, $digit3)
{
$buffer = "";
if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
{
return "";
}
if ($digit1 != "0")
{
$buffer .= convertDigit($digit1) . " hundred";
if ($digit2 != "0" || $digit3 != "0")
{
$buffer .= " and ";
}
}
if ($digit2 != "0")
{
$buffer .= convertTwoDigit($digit2, $digit3);
}
else if ($digit3 != "0")
{
$buffer .= convertDigit($digit3);
}
return $buffer;
}
function convertTwoDigit($digit1, $digit2)
{
if ($digit2 == "0")
{
switch ($digit1)
{
case "1":
return "ten";
case "2":
return "twenty";
case "3":
return "thirty";
case "4":
return "forty";
case "5":
return "fifty";
case "6":
return "sixty";
case "7":
return "seventy";
case "8":
return "eighty";
case "9":
return "ninety";
}
} else if ($digit1 == "1")
{
switch ($digit2)
{
case "1":
return "eleven";
case "2":
return "twelve";
case "3":
return "thirteen";
case "4":
return "fourteen";
case "5":
return "fifteen";
case "6":
return "sixteen";
case "7":
return "seventeen";
case "8":
return "eighteen";
case "9":
return "nineteen";
}
} else
{
$temp = convertDigit($digit2);
switch ($digit1)
{
case "2":
return "twenty-$temp";
case "3":
return "thirty-$temp";
case "4":
return "forty-$temp";
case "5":
return "fifty-$temp";
case "6":
return "sixty-$temp";
case "7":
return "seventy-$temp";
case "8":
return "eighty-$temp";
case "9":
return "ninety-$temp";
}
}
}
function convertDigit($digit)
{
switch ($digit)
{
case "0":
return "zero";
case "1":
return "one";
case "2":
return "two";
case "3":
return "three";
case "4":
return "four";
case "5":
return "five";
case "6":
return "six";
case "7":
return "seven";
case "8":
return "eight";
case "9":
return "nine";
}
}
How to convert number to string(letter) in php?
Here is the example....
<form method='POST' action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" >
Enter the Number:
<input type="number" max="999999999" name="number" required="required">(max value:999999999)<br/><input type="submit" value="Submit" name="Submit"><br/></form>
<?php
if(isset($_POST['Submit'])){
class N2L{
var $number = array( 0 => 'zero',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',20 => 'twenty',
30 => 'thirty',40 => 'forty',50 => 'fifty',60 => 'sixty',70 => 'seventy',80 => 'eighty',90 => 'ninety',
100 => 'hundred',1000 => 'thousand',100000 => 'lakh',10000000 => 'crore');
function index($numb){
if($numb<100){
return $this->upto_99($numb);
}elseif($numb<1000){
return $this->upto_999($numb);
}elseif($numb<100000){
return $this->upto_99999($numb);
}elseif($numb<10000000){
return $this->upto_9999999($numb);
}else{
return $this->upto_999999999($numb);
}
}
function upto_999999999($numb){
$number= $this->number;
if($numb<=200000000){
$crores=(int)($numb/10000000);
$str=" ".$number[$crores]." ".$number[10000000];
}else{
$crores=(int)($numb/100000000)*10;
$crores_odd=(int)($numb%100000000)/10000000;
$str=" ".$number[$crores]." ".$number[$crores_odd]." ".$number[10000000];
}
$tens_old=(int)($numb%10000000);
$str.=" ".$this->upto_9999999($tens_old);
return " ".$str;
}
function upto_9999999($numb){
$number= $this->number;
if($numb<=2000000){
$lakhs=(int)($numb/100000);
$str=" ".$number[$lakhs]." ".$number[100000];
}else{
$lakhs=(int)($numb/1000000)*10;
$lakhs_odd=(int)($numb%1000000)/100000;
$str=" ".$number[$lakhs]." ".$number[$lakhs_odd]." ".$number[100000];
}
$tens_old=(int)($numb%100000);
$str.=" ".$this->upto_99999($tens_old);
return " ".$str;
}
function upto_99999($numb){
$number= $this->number;
if($numb<=20000){
$thousands=(int)($numb/1000);
$str=" ".$number[$thousands]." ".$number[1000];
}else{
$thousands=(int)($numb/10000)*10;
$thousands_odd=(int)($numb%10000)/1000;
$str=" ".$number[$thousands]." ".$number[$thousands_odd]." ".$number[1000];
}
$tens_old=(int)($numb%1000);
$str.=" ".$this->upto_999($tens_old);
return " ".$str;
}
function upto_999($numb){
$number= $this->number;
if($numb>99 && $numb<1000){
$hundreds=(int)($numb/100);
$str=" ".$number[$hundreds]." ".$number[100];
$tens_old=(int)($numb%100);
$str.=" ".$this->upto_99($tens_old);
return " ".$str;
}
}
function upto_99($numb){
$number= $this->number;
if($numb>20 && $numb<100){
$tens=(int)($numb/10)*10;
$units=$numb%10;
return $number[$tens]." ".$number[$units];
}
else{
return " ".$number[$numb];
}
}
}
$leter=new N2L();
echo "The number ".$_POST['number']." in Words is ".$leter->index($_POST['number']);
}
?>
PHP - How to change numbers in a string to letters
Your question does not show any effort, however the following might come useful to you:
It very much depends how long your numbers would be? Assuming 0 to 9, you would do this:
$numbers = array(0,1,2,3,4,5,6,7,8,9);
$number_words = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
$string = "I have 3 apples.";
$new_string = str_replace($numbers, $number_words, $string);
The above solution is for simple words and replacements.
For instance, for numbers such as 1995445 you should then search for functions one the internet (or write one) which would convert numbers to string.
Here is a good function to do this:
http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
What we do, is first extract the number from the string:
$rule = "/([0-9]+)/";
$string = "I have 2 mobile phones, each containing 2500 messages";
$num_match;
Then we loop through the string. Each time we only replace the first occurred number, capture it, pass it to our number_to_string()
function and then get the string, use that returned string in our replace function which is preg_replace(). We utilize preg_replace()
's $limit
param to limit replacement only to first occurrence on each iteration:
while( preg_match($rule, $string, $num_match) )
{
$string = preg_replace("/".$num_match[0]."/", number_to_string($num_match[0]), $string, 1);
}
echo $string;
What I get in my browser is then:
I have two mobile phones, each containing two thousands and five hundred messages
How to convert number into word
Can you try this one.
function createFullWordOrdinal($number)
{
$ord1 = array(1 => "first", 2 => "second", 3 => "third", 5 => "fifth", 8 => "eight", 9 => "ninth", 11 => "eleventh", 12 => "twelfth", 13 => "thirteenth", 14 => "fourteenth", 15 => "fifteenth", 16 => "sixteenth", 17 => "seventeenth", 18 => "eighteenth", 19 => "nineteenth");
$num1 = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen");
$num10 = array("zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety");
$places = array(2 => "hundred", 3 => "thousand", 6 => "million", 9 => "billion", 12 => "trillion", 15 => "quadrillion", 18 => "quintillion", 21 => "sextillion", 24 => "septillion", 27 => "octillion");
$number = array_reverse(str_split($number));
if ($number[0] == 0)
{
if ($number[1] >= 2)
$out = str_replace("y", "ieth", $num10[$number[1]]);
else
$out = $num10[$number[1]]."th";
}
else if (isset($number[1]) && $number[1] == 1)
{
$out = $ord1[$number[1] . $number[0]];
}
else
{
if (array_key_exists($number[0], $ord1))
$out = $ord1[$number[0]];
else
$out = $num1[$number[0]]."th";
}
if((isset($number[0]) && $number[0] == 0) || (isset($number[1]) && $number[1] == 1))
{
$i = 2;
}
else
{
$i = 1;
}
while ($i < count($number))
{
if ($i == 1)
{
$out = $num10[$number[$i]] . " " . $out;
$i++;
}
else if ($i == 2)
{
$out = $num1[$number[$i]] . " hundred " . $out;
$i++;
}
else
{
if (isset($number[$i + 2]))
{
$tmp = $num1[$number[$i + 2]] . " hundred ";
$tmpnum = $number[$i + 1].$number[$i];
if ($tmpnum < 20)
$tmp .= $num1[$tmpnum] . " " . $places[$i] . " ";
else
$tmp .= $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " ";
$out = $tmp . $out;
$i+=3;
}
else if (isset($number[$i + 1]))
{
$tmpnum = $number[$i + 1].$number[$i];
if ($tmpnum < 20)
$out = $num1[$tmpnum] . " " . $places[$i] . " " . $out;
else
$out = $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " " . $out;
$i+=2;
}
else
{
$out = $num1[$number[$i]] . " " . $places[$i] . " " . $out;
$i++;
}
}
}
return $out;
}
This will give you the output you want.
createFullWordOrdinal(1) ----> first
createFullWordOrdinal(2) ----> second
createFullWordOrdinal(3) ----> third
createFullWordOrdinal(4) ----> fourth
Related Topics
How to Bind Multiple Parameters to MySQLi Query
Separate Space-Delimited Words in a String
What Does Double Question Mark () Operator Mean in PHP
Artisan Migrate Could Not Find Driver
Convert Utc Dates to Local Time in PHP
Why and How to Use Anonymous Functions in PHP
Namespace in PHP Codeigniter Framework
PHP Unexpected Result of Float to Int Type Cast
Getting a Checkbox Array Value from Post
Password_Hash Returns Different Value Every Time
Is There a Good PHP Geolocation Service
How to Trim White Spaces of Array Values in PHP
Laravel Eloquent: How to Order Results of Related Models
Converting a Number Base 10 to Base 62 (A-Za-Z0-9)