How to Convert Numbers into Words in Laravel 10

Hey there, Laravel enthusiasts! Today, I'm excited to walk you through a fascinating journey – turning those plain numbers into expressive words using Laravel 10. Whether you're working on invoices, receipts, or any project requiring a human touch, this guide is your key to making numeric data more readable.

In this article, we'll see how to display numbers to string/words in Laravel 8, Laravel 9, and Laravel 10. Also, you can convert numbers to words in currency or display the amount and total amount in words in laravel 10.

You can convert numbers of currency (Indian Rupee, US dollar, Euro, Pound) into word conversion in web development.

So, let's dive in, step by step, and add numbers to words in PHP and laravel 10 to convert numbers into words.

Step 1: Install Laravel 10

First things first, let's install laravel for number-to-words conversion. open your terminal and run:

composer create-project --prefer-dist laravel/laravel laravel-number-to-words

 

Step 2: Create a Helper Function

To start, let's create a helper function. In your app/helpers.php file, add the following function:

app/helpers.php

function numberToWords($number) {
    $words = [
        0 => 'Zero',
        1 => 'One',
        2 => 'Two',
        // ... add more as needed
    ];

    return $words[$number] ?? '';
}

 

Step 3: Utilizing the Helper Function

Now that we have our helper function, let's use it in our controller or wherever needed:

use Illuminate\Support\Facades\View;

public function convertNumberToWords() {
    $number = 12345;
    $words = numberToWords($number);

    return View::make('home', ['words' => $words]);
}

Method 2:

app/Http/Controllers/UsersController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class UserController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $word1 = $this->numberToWord(77);
        print($word1);
  
        $word2 = $this->numberToWord(7700);
        print($word2);
  
        $word3 = $this->numberToWord(77777);
        print($word3);
    }
    /**
     * Write code on Method techsolutionstuff
     *
     * @return response()
     */
    public function numberToWord($num = '')
    {
        $num    = ( string ) ( ( int ) $num );
        
        if( ( int ) ( $num ) && ctype_digit( $num ) )
        {
            $words  = array( );
             
            $num    = str_replace( array( ',' , ' ' ) , '' , trim( $num ) );
             
            $list1  = array('','one','two','three','four','five','six','seven',
                'eight','nine','ten','eleven','twelve','thirteen','fourteen',
                'fifteen','sixteen','seventeen','eighteen','nineteen');
             
            $list2  = array('','ten','twenty','thirty','forty','fifty','sixty',
                'seventy','eighty','ninety','hundred');
             
            $list3  = array('','thousand','million','billion','trillion',
                'quadrillion','quintillion','sextillion','septillion',
                'octillion','nonillion','decillion','undecillion',
                'duodecillion','tredecillion','quattuordecillion',
                'quindecillion','sexdecillion','septendecillion',
                'octodecillion','novemdecillion','vigintillion');
             
            $num_length = strlen( $num );
            $levels = ( int ) ( ( $num_length + 2 ) / 3 );
            $max_length = $levels * 3;
            $num    = substr( '00'.$num , -$max_length );
            $num_levels = str_split( $num , 3 );
             
            foreach( $num_levels as $num_part )
            {
                $levels--;
                $hundreds   = ( int ) ( $num_part / 100 );
                $hundreds   = ( $hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ( $hundreds == 1 ? '' : 's' ) . ' ' : '' );
                $tens       = ( int ) ( $num_part % 100 );
                $singles    = '';
                 
                if( $tens < 20 ) { $tens = ( $tens ? ' ' . $list1[$tens] . ' ' : '' ); } else { $tens = ( int ) ( $tens / 10 ); $tens = ' ' . $list2[$tens] . ' '; $singles = ( int ) ( $num_part % 10 ); $singles = ' ' . $list1[$singles] . ' '; } $words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_part ) ) ? ' ' . $list3[$levels] . ' ' : '' ); } $commas = count( $words ); if( $commas > 1 )
            {
                $commas = $commas - 1;
            }
             
            $words  = implode( ', ' , $words );
             
            $words  = trim( str_replace( ' ,' , ',' , ucwords( $words ) )  , ', ' );
            if( $commas )
            {
                $words  = str_replace( ',' , ' and' , $words );
            }
             
            return $words;
        }
        else if( ! ( ( int ) $num ) )
        {
            return 'Zero';
        }
        return '';
    }
}

 

Step 4: Displaying the Result

In your blade view, echo out the $words variable:

<h1>Transforming Numbers to Words in Laravel 10 - Techsolutionstuff</h1>
<h6>{{ $words }}</h6>

Output:

Seventy Seven
Seventy Seven Hundred
Seventy Seven Thousand Seven Hundred Seventy Seven
 
Conclusion:

In this exploration of converting numbers into words in Laravel 10, we've successfully crafted a simple and efficient solution without relying on external packages.

Showcases the transformation of numeric values into expressive and human-readable words. You can easily convert any numbers to words or letters in Laravel PHP.

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS