Integer to Words Converter in Laravel

- Bhavik Bamania

Whenever you are working on a project at times you need a package which helps to convert the numbers into words as you type in and you started to think Gosh, how can I do that? Well, I am not saying there isn't any way to do that but all of the ways are kind of hectic where you have to put your logic on own. There is nothing like one-liner especially when you are working on Laravel. By this post, I will drive you through one such package which literally transforms integer supplied to it into words.

I was working on one project, related to finance and there I have the requirement to display integers in words like most of the finance application has. And I was like man, who's gonna write such logic to convert given integers into words and that too repetitively and that too with lots of use cases and testing just for a helper function. Well, I am certainly not that kind of guy. Programmers tend to be lazy, and I am one of them.

So, I took it up Google to find out what I can have to fix the problem I am getting, I was looking for an easy way around and then I got this package IntegerToEnglish, which is indeed a very simple and straight problem-solving package, and certainly, in Laravel this seems to be a fine package for which I guess most of the developers are in hunt. So, let's dive deep and check this out.

Like any other Laravel package you need to install it by hitting this command via composer:

composer require sujalpatel/inttoenglish

And you are all set to go. Use it like this way:

IntToEnglish::Int2Eng(1000); // One Thousand IntToEnglish::Int2Eng(10500); // Ten Thousand Five hundred

and voila! You have just converted an integer value into words in just one line. Now, let's have an example to illustrate it better way.

Controller

use Illuminate\Http\Request; use SujalPatel\IntToEnglish\IntToEnglish; class TestController extends Controller { public function index() { echo IntToEnglish::Int2Eng(4500000); //Four Million Five Hundred Thousand } }

Route

Route::get('/', 'TestController@index');

and now when you hit http://localhost:8000 you will see the result.

  • Laravel
  • PHP
  • Programming