How to show all the variables as proper currency format in laravel

0 votes

I am using currency format in lots of places in my blade files. I am using number_format to show a proper currency format. So it looks like this

<p>${{ number_format($row->nCashInitialBalance, 2) }}</p> // $1,123.00
<p>${{ number_format($row->nCashCalculatedBalance, 2) }}</p> // $300.50
<p>${{ number_format($row->nCashPaymentsReceived, 2) }}</p> // $2,341.15
<p>${{ number_format($row->nCardFinalBalance, 2)}}</p> // $234.10

if I don't use it it looks like this

<p>${{ $row->nCashInitialBalance }}</p> // $1123
<p>${{ $row->nCashCalculatedBalance }}</p> // $300.5
<p>${{ $row->nCashPaymentsReceived }}</p> // $2341.15
<p>${{ $row->nCardFinalBalance }}</p> // $234.1

Also for the input fields I am using toFixed(2) in lots of places.

#nDiscount_fixed").val() = parseFloat( $("#nDiscount_fixed").val()).toFixed(2);

Isn't there an easiest way to show all the variables as proper currency format? 

Nov 2, 2020 in Laravel by kartik
• 37,520 points
3,246 views

1 answer to this question.

0 votes

Hello,

You could create a custom Laravel directive. You still need to call that directive at every place you need it but comes with the benefit that if you ever want to change the code (e.g. replace number_format with something else) you only have to update that directive.

Example (taken from the docs and updated for your use case) (in your AppServiceProvider boot method):

Blade::directive('convert', function ($money) {
    return "<?php echo number_format($money, 2); ?>";
});

To use it in Blade:

@convert($var)
answered Nov 2, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,880 points
3,229 views
0 votes
1 answer

How to get all pending jobs in laravel queue on redis?

Hello @kartik, Here is the way I do ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,880 points
9,481 views
0 votes
1 answer

How to get the id when you're validating in the model Validation unique on update using laravel?

Hello @kartik, in Laravel's inbuilt auth system, the ...READ MORE

answered Sep 11, 2020 in Laravel by Niroj
• 82,880 points
6,655 views
0 votes
1 answer

How to set .env values in laravel programmatically on the fly?

Hello @kartik, Since Laravel uses config files to ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,880 points
4,926 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,878 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,932 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,866 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,218 views
0 votes
1 answer
0 votes
1 answer

How to get all the users except current logged in user in laravel eloquent?

Hello @kartik, You can get the current user's ...READ MORE

answered Dec 8, 2020 in Laravel by Niroj
• 82,880 points
7,362 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP