Inside this article we will see conversion of a International number into Indian format number. Let’s consider a CodeIgniter 4 application and we want to do it. So what’s the plan?
Need to create a helper function, load it and use anywhere inside the application either inside any of views or inside into controllers.
Learn More –
- CodeIgniter 4 Multiple Databases And Connection Groups
- CodeIgniter 4 Pagination Service or Library
- CodeIgniter 4 Pagination with Search Filter Tutorial
- CodeIgniter 4 PDF Generate Tutorial
Let’s get started.
Number System Chart
We have few examples here. Have a look into the complete chart.
International Numeric System
Indian Numeric System
Let’s see the things in CodeIgniter 4 by making a custom helper.
CodeIgniter 4 Installation
To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.
composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
Environment (.env) Setup
When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env
Either we can do via renaming file as simple as that. Also we can do by terminal command.
Open project in terminal
cp env .env
Above command will create a copy of env file to .env file. Now we are ready to use environment variables.
Enable Development Mode
CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.
Open .env file from root.
# CI_ENVIRONMENT = production
// Do it to
CI_ENVIRONMENT = development
Now application is in development mode.
Create CodeIgniter 4 Helper
Helpers contain standalone functions which can be used through out application to complete any specific task.
To create CodeIgniter 4 helper, need to create a PHP file into folder /app/Helpers.
Let’s create a file named as numsystem_helper.php. Keep in mind we have concatenated _helper with the name of helper file.
// Checking function already exists or not if(!function_exists("indian_system")){ function indian_system($value){ $len = strlen($value); $m = ''; $value = strrev($value); for($i=0;$i<$len;$i++){ if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$len){ $m .=','; } $m .=$value[$i]; } return strrev($m); } }
We have created a function inside that helper file. To use this, first we need to load and then we can freely use it.
Load CodeIgniter 4 Helper
We have two options available to load helpers in application. Either we can use BaseController.php means parent controller or we can load into individual controller.
//... // find $helpers into controller and load custom helper protected $helpers = ["numsystem"]; //...
Keep in mind, we haven’t included _helper while loading into application.
If we want to load in any specific individual controller, need to use helper() function.
<?php namespace App\Controllers; class Site extends BaseController { public function number_system() { helper(["numsystem"]); echo indian_system(10000); // 10,000 } }
Using Helper for Indian Number Format
After loading helper into application. Now we have an access to use it in a free way. If we are in any view file, simply pass your number value into the function.
Example: 1000000
indian_system(1000000);
Output: 10,00,000
We hope this article helped you to learn about Convert Number From International System To Indian System in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.