我发现了一个关于printf('%50d',33)作为标题的问题,我只希望它显示00033,但它显示000335。
我试着把printf('%50d-',33)变成00033-6,如果没有错,最后一个数字就是总数字计数。
我能知道怎么把它去掉吗?
编辑的
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>
发布于 2017-08-25 11:49:40
问题解决了。使用sprintf("A%'.05d",$this->id);实例的printf("A%'.05d",$this->id);
谢谢@Matt的链接。更新问题帖子的答案。
发布于 2017-08-25 10:14:53
看起来您的格式字符串与提到过 by 哑光的顺序不一致。应该是printf("%'.05d\n",33);
https://stackoverflow.com/questions/45878566
复制相似问题