I am fairly new in Laravel and Blade templating.
Can anyone help show me how to do this?
I have a view like this:
@foreach ($Expenses as $Expense)
<tr>
<td>{{{ $Expense->type }}}</td>
<td>{{{ $Expense->narration }}}</td>
<td>{{{ $Expense->price }}}</td>
<td>{{{ $Expense->quantity }}}</td>
<td>{{{ $Expense->amount }}}</td>
</tr>
@endforeach
I want the $Expense->price
and$Expense->amount
to be formatted.
I tried using it on the $Expense->amount
as number_format($Expense->amount)
but it didn't work.
PHP's number_format() function gives you an easy way to format numbers for displaying to the user. You can separate thousands with commas or other separators, choose the decimal point character, and choose the number of decimal places to display (or display no decimals at all).
PHP | number_format() Function $number: It is required parameter which specified the number to be formatted. If no other parameters are set, the number will be formatted without decimals and with the comma (, ) as the thousands separator. $decimals: It is optional parameter and used to specifies decimals.
$twoDecNum = sprintf('%0.2f', round($number, 2)); The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding. Show activity on this post. This will give you 2 number after decimal.
This should work :
<td>{{ number_format($Expense->price, 2) }}</td>
If you are using Eloquent, in your model put:
public function getPriceAttribute($price)
{
return $this->attributes['price'] = sprintf('U$ %s', number_format($price, 2));
}
Where getPriceAttribute is your field on database. getSomethingAttribute.
If you are using Eloquent the best solution is:
public function getFormattedPriceAttribute()
{
return number_format($this->attributes['price'], 2);
}
So now you must append formattedPrice in your model and you can use both, price (at its original state) and formattedPrice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With