Hey there, fellow developers! Ever found yourself in a situation where you needed to transform a bunch of numbers into words? Maybe you're working on generating invoices, writing checks, or dealing with any scenario where presenting numeric values in plain English is a must.
Well, guess what? Laravel 10 has got our backs with its SpellNumber functionality. Today, I'm excited to take you through a step-by-step guide on how to effortlessly convert those digits into letters, adding a touch of human-readable magic to our numeric world.
In this article, we'll see convert numbers to letters using spellNumber, how to convert numbers to words in laravel 8/9/10, how to convert numbers to text in laravel 9/10, numbers conversion to string.
you can convert numbers to words in various languages and also obtain the value in currency format according to the selected language.
Supported languages include English, Spanish, Portuguese, French, Italian, Romanian, Hindi, Polish and Persian (Farsi).
So, grab your coding gear, and let's dive into the simplicity of turning numbers into words with Laravel 10! 🚀
Step-by-Step Guide: Converting Numbers to Letters in Laravel 10 using SpellNumber
If you haven't already installed Laravel 10, you can do so using Composer. Open your terminal and run the following command:
composer create-project laravel/laravel my-project
To install the dependency via Composer, execute the following command:
composer require rmunate/spell-number
This is optional.
You can publish the config file with:
php artisan vendor:publish --provider="Rmunate\\Utilities\\Providers\\SpellNumberProvider" --tag="config"
Navigate to your Laravel project directory and create a new controller. You can use the following Artisan command:
php artisan make:controller SpellNumberController
This will generate a new controller file at app/Http/Controllers/SpellNumberController.php
.
Open SpellNumberController.php
and add the following code to implement the SpellNumber
functionality:
<?php
namespace App\Http\Controllers;
use Rmunate\Utilities\SpellNumber;
class SpellNumberController extends Controller
{
public function convertToWords()
{
SpellNumber::value(100)->locale('en')->toLetters();
// "one hundred"
SpellNumber::value(100)->locale('es')->toLetters();
// "cien"
SpellNumber::value(100)->locale('fa')->toLetters();
// "صد"
SpellNumber::value(100)->locale('hi')->toLetters();
// "एक सौ"
}
}
You can easily convert whole numbers to words by defining the locale to be applied. If you do not define a locale, "en" (English) will be applied by default. Remember that if you export the vendor configuration file, it will not require this definition.
Floating Point
If required, you can pass a floating point number as an argument to convert it to words. Only the first two digits after the floating point will be taken.
use Rmunate\Utilities\SpellNumber;
SpellNumber::value(123456789.12)->locale('en')->toLetters();
// "one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine and twelve"
SpellNumber::value(123456789.12)->locale('es')->toLetters();
// "ciento veintitrés millones cuatrocientos cincuenta y seis mil setecientos ochenta y nueve con doce"
SpellNumber::value(123456789.12)->locale('hi')->toLetters();
// "बारह करोड़ चौंतीस लाख छप्पन हज़ार सात सौ नवासी और बारह"
It is very easy to use this solution for systems where you require the value more than in letters, with the specified currency structure. If you require that an integer have this transformation, you can do it in the following way.
SpellNumber::value(100)->locale('en')->currency('Dollars')->toMoney();
// "one hundred dollars"
SpellNumber::value(100)->locale('es')->currency('Pesos')->toMoney();
// "cien pesos"
SpellNumber::value(100)->locale('fa')->currency('تومان')->toMoney();
// "صد تومان"
SpellNumber::value(100)->locale('hi')->currency('रूपये')->toMoney();
// "एक सौ रूपये"
Floating Point
You can also pass a floating point number as an argument to convert it into words in currency format. Only the first two digits after the floating point will be taken.
This method can be useful for invoices, receipts, and similar scenarios. Obtain the supplied value in currency format.
SpellNumber::value(100.12)->locale('en')->currency('Dollars')->fraction('Cents')->toMoney();
// "one hundred dollars and twelve cents"
SpellNumber::value(100.12)->locale('es')->currency('Pesos')->fraction('Centavos')->toMoney();
// "cien pesos con doce centavos"
SpellNumber::value(100.12)->locale('hi')->currency('रूपये')->fraction('पैसे')->toMoney();
// "एक सौ रूपये और बारह पैसे"
SpellNumber::value(100.65)->locale('pl')->currency('złotych')->fraction('groszy')->toMoney();
// "sto złotych i sześćdziesiąt pięć groszy"
You may need to list some data within your system, if so, this section is your solution.
This package included the option to use ordinal numbers for this purpose.
You can only use this method with integers, remember to ensure that the value meets this type, otherwise we may have unexpected responses.
use Rmunate\Utilities\SpellNumber;
SpellNumber::value(2)->locale('en')->toOrdinal();
// "second"
SpellNumber::value(2)->locale('en')->toOrdinal(SpellNumber::ORDINAL_DEFAULT);
// "second"
SpellNumber::value(2)->locale('es')->toOrdinal(SpellNumber::ORDINAL_MALE);
// "segundo"
SpellNumber::value(2)->locale('es')->toOrdinal(SpellNumber::ORDINAL_FEMALE);
// "segunda"
Especific Locale
If you want to use a specific locale, you should always use the constant SpellNumber::SPECIFIC_LOCALE
use Rmunate\Utilities\SpellNumber;
SpellNumber::value(2)->locale('en_US', SpellNumber::SPECIFIC_LOCALE)->toOrdinal();
// "second"
SpellNumber::integer(2)->locale('es_MX', SpellNumber::SPECIFIC_LOCALE)->toOrdinal(SpellNumber::ORDINAL_FEMALE)
// "segunda"
Open the routes/web.php
file and define a route to invoke the convertToWords
method in your SpellNumberController
:
use App\Http\Controllers\SpellNumberController;
Route::get('/convert-to-words', [SpellNumberController::class, 'convertToWords']);
Start your Laravel development server by running the following command in your terminal:
php artisan serve
And there you have it! You've successfully set up a Laravel project and integrated the SpellNumber functionality to convert numbers to letters.
You might also like: