DEV Community

loading...
Cover image for My way to handle money format input in Laravel

My way to handle money format input in Laravel

suryawiguna profile image Surya Wiguna ・2 min read

Common usage of website is to handle data about finance, which is the most data is money and it has a format. In Indonesia for example the format is like Rp1.500.000,00. The problem is with default HTML input, it can't show format like that by default. So how to handle it? At least here's my may to do that in Laravel based website.

Database side

It has to handle in three side, the first in inside the database where you want to store the data. On sql there is decimal type that you can use to store number with commas. So in the migration you can simply use

$table->decimal('amount', 8, 2);
Enter fullscreen mode Exit fullscreen mode

8 is the total digits and 2 is decimal digits. Refer to the official documentation.

HTML side

So after we set database column to proper type, next we need to set the html input to prevent unwanted data that user accidentally input. In html there is

<input type="number">
Enter fullscreen mode Exit fullscreen mode

to handle number input. You can use attribute step to define decimal digit right on the input. There're other additional attributes that you can use to, find more here

Laravel Blade side

Lastly, when you want to show the data to the user. Data format from database usually will look like this 1500000.00 and you want to show it like this 1.500.000,00. To do this you can use php number_format() function inside the blade.

number_format($data, 2, ',', '.');
Enter fullscreen mode Exit fullscreen mode

The first parameter is your number, next is how many decimal digit you want to show, the third is decimal separator, and the last is thousand separator. Read more in PHP manual

Those are my way to handle money data, thanks for reading this and if you want to say something leave it in discussion.

Discussion (0)

pic
Editor guide