Concept of Number Helper in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
11 Views

Number Helper is a pre defined helper in CodeIgniter 4. It provides few functions by the help of which we can work easily with numeric data values.

Inside this article, we will see the concept of Number helper in CodeIgniter 4. This whole tutorial will clear you about loading number helper, available functions and their way of using it.

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.


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.


Settings Environment Variables

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.


What is Number Helper?

Number helper provides several functions which works with numeric data value and works for basic general operations.

This helper file number_helper.php we can find at /vendor/codeigniter4/framework/system/Helpers folder.

Here, we have the functions available when we use number helper.

  • number_to_amount()
  • number_to_currency()
  • number_to_roman()
  • number_to_size()

First before using any functions, we need to load number helper and then we can access all of it’s functions.


Loading Number Helper

We can load number helper in CodeIgniter 4 in two different ways. Methods to load by –

  • By BaseController.php [parent controller]
  • Loading into any specific controller

Loading by Parent Controller

Open BaseController.php file from /app/Controllers folder and search for $helpers. Add helper into it.

protected $helpers = ["number"];

Loading into Specific Controller

Open any application controller, go inside method and write this code to load number helper.

helper("number");

OR

helper(["number"]);

Number Helper Functions

We will see all number functions here.

number_to_size()

Formats numbers as bytes, based on size, and adds the appropriate suffix.

Syntax

number_to_size($num[, $precision = 1[, $locale = null])

Example

echo number_to_size(456); // Returns 456 Bytes 

echo number_to_size(4567); // Returns 4.5 KB

echo number_to_size(12345678912345); // Returns 1.8 GB 

echo number_to_size(123456789123456789); // Returns 11,228.3 TB

An optional second parameter allows you to set the precision of the result –

echo number_to_size(45678, 2); // Returns 44.61 KB

number_to_amount()

It converts a number into a human-readable version.

Syntax

number_to_amount($num[, $precision = 1[, $locale = null])

Example

echo number_to_amount(123456); // Returns 123 thousand  

echo number_to_amount(123456789); // Returns 123 million  

echo number_to_amount(1234567890123, 2); // Returns 1.23 trillion 

echo number_to_amount('123,456,789,012', 2); // Returns 123.46 billion

An optional second parameter allows you to set the precision of the result:

echo number_to_amount(45678, 2); // Returns 45.68 thousand

number_to_currency()

Converts a number in common currency formats, like USD, EUR, GBP, etc.

Syntax

number_to_currency($num, $currency[, $locale = null])

Example

echo number_to_currency(1234.56, 'USD');  // Returns $1,234.56 

echo number_to_currency(1234.56, 'EUR');  // Returns €1,234.56 

echo number_to_currency(1234.56, 'GBP');  // Returns £1,234.56 

echo number_to_currency(1234.56, 'YEN');  // Returns YEN1,234.56

number_to_roman()

It converts a number into roman value.

Syntax

number_to_roman($num)

Example

echo number_to_roman(23);  // Returns XXIII 

echo number_to_roman(324);  // Returns CCCXXIV 

echo number_to_roman(2534);  // Returns MMDXXXIV

We hope this article helped you to Complete Concept of Number helper in CodeIgniter 4 Tutorial. 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

Leave a Comment