How to Convert Number to Words in Laravel?

By Hardik Savani August 30, 2022 Category : Laravel

Hi Dev,

In this example, I will show you how to convert number to words in laravel. I explained simply step by step how to convert number to words in laravel blade. It's a simple example of laravel convert number to words. you'll learn how to convert number to words in laravel blade example. Let's get started with how to convert number to words in laravel.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.

Sometimes, we need to convert number into words like 100 into Hundred or 1000 into Thousand etc. So how you will do with laravel? Don't worry, I will give you two ways to convert numbers into words example. In the first example, we will create a simple controller function and convert numbers into words. In the second example, we will create a custom blade directive to convert numbers into words in the blade file. you can see both examples and use whatever you want.

So, Without any further ado, let's see below code example.

Example 1:

In this example, we will create DemoController with numberToWord() method to convert number into words. so you can see the below code with output:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$word = $this->numberToWord(120);

print($word);

$word = $this->numberToWord(4500);

print($word);

$word = $this->numberToWord(58010);

print($word);

}

/**

* Write code on Method

*

* @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 '';

}

}

Output:

One Hundred Twenty

Four Thousand and Five Hundreds

Fifty Eight Thousand and Ten

Example 2:

In second example, we will create custom blade directive in AppServiceProvider service provide file. we will create @numberToWord() directive for convert number into word. so you can see below code with output:

app/Provides/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Facades\Blade;

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*

* @return void

*/

public function register()

{

}

/**

* Bootstrap any application services.

*

* @return void

*/

public function boot()

{

Blade::directive('numberToWord', function ($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 );

}

}else if( ! ( ( int ) $num ) ){

$words = 'Zero';

}else{

$words = '';

}

return $words;

});

}

}

Use in Blade File:

<p>@numberToWord(1200)</p>

Output:

One Thousand and Two Hundreds

I hope it can help you...

Tags :