php printf 5 digit but show extra 1 digit

732 views Asked by At

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>

2

There are 2 answers

0
Kenny Ong On BEST ANSWER

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.

1
Marc Lauder On

Looks like your format string is not in the right order as mentioned by Matt. Should be printf("%'.05d\n",33);