In many financial applications, there’s often a need to convert numerical amounts into words, especially in invoicing and billing systems. While it’s easy for humans to read numbers, there are situations where displaying amounts in words makes it more clear and formal, such as for official documents. In this blog post, we will walk you through a PHP solution to convert numbers into words for Indian currency (rupees and paise), but the method can easily be adapted to handle other currencies like dollars and cents.
Why Convert Numbers to Words?
Converting numbers into words is crucial for several reasons:
- Legibility: It helps avoid confusion or misinterpretation when reading numbers.
- Legal and formal documents: Cheques, invoices, and contracts often require amounts to be written in words to ensure clarity.
- Professionalism: Writing numbers in words can add a layer of professionalism to your application, especially in the financial sector.
The PHP Solution for Converting Numbers into Words
Here’s the PHP code that converts a given amount in Indian rupees and paise into words. This can be particularly useful for Indian accounting or invoicing systems.
<?php
function convertPriceToWords($price)
{
$price = number_format($price, 2, '.', '');
$num = explode(".", $price);
$dollars = $num[0];
$cents = isset($num[1]) ? $num[1] : '00';
$words = convertNumberToWords($dollars) . " rupees";
if ($cents != '00') {
$words .= " and " . convertNumberToWords($cents) . " paise";
}
return ucfirst($words);
}
function convertNumberToWords($number)
{
$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(
20 => "twenty",
30 => "thirty",
40 => "forty",
50 => "fifty",
60 => "sixty",
70 => "seventy",
80 => "eighty",
90 => "ninety"
);
if ($number == 0) return 'zero';
if ($number < 20) {
return $ones[$number];
}
if ($number < 100) {
$tensValue = floor($number / 10) * 10;
$onesValue = $number % 10;
return $tens[$tensValue] . ($onesValue ? " " . $ones[$onesValue] : '');
}
if ($number < 1000) {
return $ones[floor($number / 100)] . " hundred " . convertNumberToWords($number % 100);
}
if ($number < 1000000) {
return convertNumberToWords(floor($number / 1000)) . " thousand " . convertNumberToWords($number % 1000);
}
if ($number < 1000000000) {
return convertNumberToWords(floor($number / 1000000)) . " million " . convertNumberToWords($number % 1000000);
}
if ($number < 1000000000000) {
return convertNumberToWords(floor($number / 1000000000)) . " billion " . convertNumberToWords($number % 1000000000);
}
return '';
}
echo convertPriceToWords(45000.95);
How the Code Works:
convertPriceToWords($price)
: This function takes in a price and first formats it to two decimal places. It then splits the price into the whole number part (rupees) and the decimal part (paise). The whole number and decimal parts are then converted into words.convertNumberToWords($number)
: This function converts a given number into words, handling values from one to a billion. It breaks the number into thousands, millions, and billions and recursively converts them into words.- Handling Decimal Points (Paise/Cents): If there are cents (or paise) in the number, it appends the paise value to the rupees. For example, if the number is
45000.95
, it will returnForty-five thousand rupees and ninety-five paise
.
Example Output:
- Input: 45000.95
- Output: Forty-five thousand rupees and ninety-five paise
Adaptable for Other Currencies:
While this example focuses on Indian currency, the function can be easily adapted to work with other currencies like dollars and cents by simply replacing the word “rupees” with “dollars” and “paise” with “cents.”
Conclusion
Converting numbers into words is a common requirement for financial applications. This PHP solution is flexible, allowing you to convert both the whole number and decimal portions into words, making it ideal for invoices, receipts, and legal documents. Whether you’re working with rupees, dollars, or any other currency, this method provides a straightforward and effective way to display numbers in a human-readable format.