Number From International System To Indian System

Share this Article
Reading Time: 4 minutes
491 Views

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.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

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.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# 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.

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.

Find More on CodeIgniter 4 here