-1

I need to convert the string to following format.

1000 -> 0000001000
10000 -> 0000010000
25000 -> 0000025000

I tried this way ceil(str_pad($range_array[0],6 , "0", STR_PAD_LEFT))

But this works for 1000, but fails for 10,000 .

Is there any function to create it ?

Ramesh
  • 2,295
  • 5
  • 35
  • 64

3 Answers3

9
printf("%010d", $number);
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
1

Not sure why you're padding with 6, or using ceil(). str_pad takes the total length of the string you want:

<?php

foreach (array(1000, 10000, 25000) as $int) {
    echo str_pad($int, 10, '0', STR_PAD_LEFT) . "\n";
}
0

You could use sprintf() to format the number nicely see the manual

 $result = sprintf( '%010.0f', $inputNumber );

Or better still

 $result = sprintf( '%010d', $inputNumber );
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149