Easily convert Numbers into Words in PHP

PHP converting numbers into words

Sometimes we need to convert numbers into words or texts in PHP application. But PHP don’t have build in functions to convert numbers into words.

Especially, sometimes we want to display currency in words. For eg. if we convert number 15,070 into words it will be Fifteen Thousands Seventy. But how can we achieve this conversion in a simple PHP way.

Well, I have a simple solution for that. I’ve made a simple PHP function for converting numbers into words. You can use to convert any type of numbers such as currency, distance, measurements etc.

Just change $numbers_name and $decimal_name variable according to your requirements.

<?php 
function NumbersToWords(float $number)
{
    // change these units according to your requirements
    $numbers_name = 'Rupees';
    $decimal_name = 'Paise';
          
    $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;
    }
    $numbers_in_words = implode('', array_reverse($str));
    $decimals_in_words = ($decimal > 0) ? " . " . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' '.$decimal_name : '';

    return ($numbers_in_words ? $numbers_in_words.' ' . $numbers_name : '') . $decimals_in_words;
}

echo NumbersToWords(870707);

?>

Use this function into your next project.

Hope you like this PHP tutorial.

If you liked this article, then please subscribe to our YouTube Channel for useful videos. You can also find us on Twitter and Facebook.

About the author

Ishwar Acharya

I am a web designer and developer and keen on contributing to the web development industry since last 10 years. I love PHP development and absolutely amazed by front-end js libraries such as React, VueJs etc. Feel free to follow me @imishwaracharya on twitter.

Write a Reply or Comment

Your email address will not be published. Required fields are marked *

No Comment

This post has not been commented yet.

The Webography