Reputation: 401
I found a problem about printf('%50d',33)
As title,
I just want it to show 00033, but it shows 000335.
i try printf('%50d-',33)
it become 00033-6
,
if not wrong last number is total digit count.
May I know how to remove that?
EDITED
Model/Product.php
class Product extends Model
{
protected $appends = ['code'];
public function getCodeAttribute(){
return $this->attributes['code'] = sprintf("A%'.05d",$this->id);
}
}
View/home.blade.php
<ul class='codeList'>
@foreach($products as $product)
<li>
<div class='name'>{{ $product->name }}</div>
<div class='code'>{{ $product->code }}</div> {{-- This Part Show A00033 --}}
</li>
@endforeach
</ul>
Upvotes: 1
Views: 758
Reputation: 401
Problem Solved.
use sprintf("A%'.05d",$this->id);
instent of printf("A%'.05d",$this->id);
Thanks @Matt for the link. Update Answer to question post.
Upvotes: 0
Reputation: 31
Looks like your format string is not in the right order as mentioned by Matt.
Should be printf("%'.05d\n",33);
Upvotes: 2