Number To Words Convert in Laravel - WebJourney
Today we will learn about number to word convertion. I will show you how to convert a number into words using laravel 10. But you can user any laravel versions. I will share you a simple solution to convert any number into words using a single helper function.
Let's see the output first:
To convert numbers to words in Laravel, you can follow these steps:
Step-1: Install Laravel
To install a new laravel app run the following code:
composer create-project laravel/laravel number-to-word
Step-2: Create NumberToWordHelper
Now create a Helper folder inside app folder. After that create a NumberToWordHelper file inside newly created Helper folder. Once the helper is created update the compser.json file to include the helper file.
composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers/NumberToWordHelper.php"
]
},
So execute the bellow artisan autoload command to load class files:
composer dump-autoload
Step-3: Update NumberToWordHelper.php
In his step we will create a helper function and write logic for the number to word convertion. Just update the helper with the given code.
NumberToWordHelper.php
<?php
function convertNumber($number)
{
$num = ( string ) ( ( int ) $number );
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;
}
Step-4: Update welcome.php
Here we will call the helper function convertNumber() and pass any number in function argument and the number will automatically change to words.
welcome.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>How to convert number to word in Laravel 10</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="offset-md-2 col-md-8">
<h4 class="text-success text-center mb-5">How to convert number to word in Laravel 10</h4>
<table class="table table-bordered">
<thead>
<tr>
<td>Number</td>
<td>Word</td>
</tr>
</thead>
<tbody>
<tr>
<td>$99</td>
<td>{{ convertNumber(99) }}</td>
</tr>
<tr>
<td>$120</td>
<td>{{ convertNumber(120) }}</td>
</tr>
<tr>
<td>$8796</td>
<td>{{ convertNumber(8796) }}</td>
</tr>
<tr>
<td>$698099</td>
<td>{{ convertNumber(698099) }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Step-5: Test App
Run the artisan serve command and hit url to test the app
php artisan serve
Paste the url in thebrowser and hit enter:
http://127.0.0.1:8000/
Hope this will help you alot.
If you like what you are reading, please think about buying us a coffee as a token of appreciation.
We appreciate your support and are committed to providing you useful and informative content.
We are thankful for your ongoing support
You May Read Bellow Articles:
Laravel 9 live search data in a table using ajax.
How to send SMS in laravel 9 using Twilio SMS API-Webjourney
Laravel 9 pdf invoice generate and download with barryvdh dompdf
How to create multi language website by laravel 9
Laravel 10 multiple form validation on the same page-WebJourney
Laravel 10 Breeze Authentication - WebJourney
Laravel 10 Ajax jQuery Crud with Pagination and Live Search
Laravel Naming Conventions Accepted by Laravel community
Laravel Shorter and More Readable Syntax - WebJourney