When creating a web application, we often encounter processing of numbers data type that we will use to store the total of a transaction, so it is easier for users to read a nominal, we will create a function with php to format a nominal whose function is to separate units, hundreds and thousands. The php function that we will use to create this rupiah format is number_format(), you can see the documentation in number_format().
When you apply the Currency format to a number, the currency symbol appears right next to the first digit in the cell. You can specify the number of decimal places that you want to use, whether you want to use a thousands separator, and how you want to display negative numbers.
What describes the currency number format?
The Currency format is used for “general monetary values” and puts the dollar sign directly in front of the left-most number. The Accounting format, on the other hand, lines up both the dollar sign as well as the decimal in the column of numbers.
Create a php function for currency format
the first step in creating this function is to create a new project in xampp by creating a new folder in xampp/htdocs with the name rupiah then create a php file which we name format_rupiah.php then fill it with the code below
<?php
//membuat function format rupiah dengan PHP
//tutorial https://webbaliseo.com
function format_rupiah($nominal)
{
$hasil_formatnya = "Rp " . number_format($nominal,2,',','.');
return $hasil_formatnya;
}
?>
After we create a function for the rupiah format, we will test the function by creating an index.php file and filling in the code below
<?php
//membuat function format rupiah dengan PHP
//tutorial https://webbaliseo.com
//kita panggil file format_rupiah
include('format_rupiah.php');
// membuat contoh nominal yang akan kita beri function format rupiah
$nominal = 1200500;
//menampilakan contoh nominal diatas
echo '<center>Nominal yang belum di beri function format rupiah '.$nominal.'</center><br>';
//memberi function format rupiah dan menampilkan hasilnya
echo '<center> Hasil setelah di beri function format rupiah : '.format_rupiah($nominal).'</center>';
?>
Run the currency format code
if we run the code above by typing localhost/rupiah/ in the browser, the result will be like the image below
Please don’t forget to turn on the xampp like images below
That’s the tutorial for making the rupiah format function in PHP, happy coding