3

I would like to have a function to generate numbers starting from "000100".

Right now I have the following function that allows me to generate numbers with leading zeros.

How can I modify it in order to fit my needs?

function lz( $aNumber, $intPart, $floatPart=NULL, $dec_point=NULL, $thousands_sep=NULL) 
{     
    $formattedNumber = $aNumber;
    if (!is_null($floatPart)) 
    {  
        $formattedNumber = number_format($formattedNumber, $floatPart, $dec_point, $thousands_sep);
    }

    $formattedNumber = str_repeat("0",($intPart + -1 - floor(log10($formattedNumber)))).$formattedNumber;
    return $formattedNumber;
}

Example: A new order has been placed and the order id is "19". The final order number must be 000119.

Thanks!

Psyche
  • 8,513
  • 20
  • 70
  • 85
  • @codaddict that's what the client wants. He wants the numbering for orders to start at 000100. – Psyche Nov 21 '11 at 19:01

4 Answers4

9

Why not use a single printf as:

printf("%06d",$number); 

See it

The format specifier used is:

% - Marks the beginning of the format specifier
0 - Ensures zero filling if number being printed has fewer digits
6 - Number of digits reserved
d - We are printing an integer

EDIT:

From your edit looks like you want to add 100 to the input before formatting it. If so you can do:

printf("%06d",$number+100); 
codaddict
  • 445,704
  • 82
  • 492
  • 529
3

Simply use str_pad:

<?php
    for($i = 100; $i < 115; $i++)
        echo str_pad($i, 6, '0', STR_PAD_LEFT) . '<br>';
?>

http://codepad.viper-7.com/cI9DTO

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
1

I wanted to see which was faster:

sprintf("%06d",$loops);

Or

str_pad($i, 6, '0', STR_PAD_LEFT)

The difference between the two, were insignificant. sprintf was constantly faster than str_pad but only by, typically, about 150 pico seconds.

Ref: Loops: 10,000    Time: 0.00033807754516602
Time Loop 1: Loops: 10,000 Time: 0.0026688575744629  Time per loop: 0.000000267
Time Loop 2: Loops: 10,000 Time: 0.0040788650512695  Time per loop: 0.000000408

Difference per loop: 0.000 000 141

Ratio Loop2/Loop1: 1.5283187421833
Ratio Loop1/Loop2: 0.65431377133505

My Benchmark Code:

When the code being benchmarked runs in less time than the loop itself, the Loop Time will significantly influence the results. So I use a reference loop to subtract the Loop Time from the test Loops.

I used loops of 10,000 iterations.

$saveLoops = 10000;

Reference Loop:

$loops = $saveLoops;
$t = microtime(true);
while ($loops > 0) {
  $loops--;
}
$refTime = microtime(true) - $t;
echo "Ref: Loops: $saveLoops Time: $refTime\n";

LOOP 1

$loops = $saveLoops;
$t = microtime(true);
while ($loops > 0) {
  $loops--;
  $n = sprintf("%06d",$loops); 

}

$t = microtime(true) - $t - $refTime;
$time['End1'] =  $t;
$t1 = $t/$saveLoops;
echo "Time Loop 1: Loops: " . number_format($saveLoops) . " Time: $t  Time per loop: " . number_format($t1,9) . "\n";

LOOP 2

$loops = $saveLoops;
$t = microtime(true);
while ($loops > 0) {
  $loops--;
  $n = str_pad($loops, 6, '0', STR_PAD_LEFT);
}
$t = microtime(true) - $t - $refTime;
$time['End2'] =  $t;
$t2 = $t/$saveLoops;
echo "Time Loop 2: Loops: " . number_format($saveLoops) . " Time: $t  Time per loop: " . number_format($t2,9) . "\n";

Calc Times

$dif = abs($t2 - $t1);
$diff = number_format($dif,9);

$diff = substr($diff,0,5) . ' ' . substr($diff,5,3). ' ' . substr($diff,8,3);

echo "\nDifference per loop: $diff\n";
echo "\nRatio Loop2/Loop1: " . ($time['End2'] / $time['End1']);
echo "\nRatio Loop1/Loop2: " . ($time['End1'] / $time['End2']);
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
0
$arrayVar = Array();
for($i=100;$i<200;$i++){
$arrayVar[$i] = "000" . $i;
}
foreach($arrayVar as $num){
echo $num . "<br>";
}

Basically just adds the 0's to the begining of the variable in the array, second part spits out the unique numbers.

Kevin Collins
  • 332
  • 1
  • 12