Concept of Number Helper in CodeIgniter 4 Tutorial

Reading Time: 4 minutes
3,154 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.

Learn More –

Let’s get started.


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.


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.

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.