Question:
php function create 000 digit in front of a number?
etjie
2007-06-11 01:32:18 UTC
Can anybody tell me the php function or modul for create / echo / print the number with leading 0 in front of the number. For example:
$var1 = 3; // expected 0003
$var2 = 50; // expected 0050
$var3 = 120; // expected 0120
$var4 = 5231; // expected 5231
?>
all i want to show on the page is they have the same max char length and when the number not have the full digit, they printed the leading 0 format.
Six answers:
mithunrockey
2007-06-11 04:46:27 UTC
This will work..... take it......






$var1 = 3;

printf("%04d",$var1);



?>
foghorn111
2007-06-11 04:23:02 UTC
I don't know the php code for this, but I have a pretty good algorithm that works fast for just about any language... here's the step by step...

1. figure out how many digits you want in your number string... in this case it looks like 4...

2. concatenate two strings together... the first one has exactly 4 zeros in it... like this $var1 = '0000'; the second one contains your number eg $var2 = 34... the resulting code should look something like this... $var3 = $var1 . $var2 (don't know the proper concatenation operator, so I used a period...)

3. select the rightmost four characters from the resultant variable... in SQL, the code would be select right(var3, 4)...



Using this algorithm, you don't have to figure out how many zeroes you need based on looping through the digits in your number...
rob
2007-06-11 06:54:30 UTC
Heres one way




$var1 = 3; // expected 0003

$var2 = 50; // expected 0050

$var3 = 120; // expected 0120

$var4 = 5231; // expected 5231





function createDigit($digit)

{

if (strlen($digit) == 1) {

$new_digit = "000".$digit;

}

elseif (strlen($digit) == 2) {

$new_digit = "00".$digit;

}

elseif (strlen($digit) == 3) {

$new_digit = "0".$digit;

}

else {

$new_digit = $digit;

}



return $new_digit;

}







echo createDigit($var2);



?>



or a shorter more automatic way would be:



function createDigit($digit)

{

$num = strlen($digit);

$max = 4;

$zeros = $max - $num;



$new_digit = str_repeat("0",$zeros)."".$digit;

return $new_digit;

}
2016-11-28 08:46:32 UTC
a variety is divisible by means of 9 if and on condition that the sum of the digits is divisible by means of 9. If we ought to apply all 4 numbers as quickly as and merely as quickly as, then there is no answer because of the fact the sum of the digits could consistently be 3+4+5+6 = 18. If we are allowed to repeat digits, then the main important variety we could make is 6666, the sum of whose digits is 24. it particularly isn't divisible by means of 9, for this reason the respond is 6666.
2007-06-11 01:50:09 UTC
You need to display the numbers as strings using text formatting, or you can use the string length and string concatenation to add the 0s.
thetexaspsycho2003
2007-06-11 08:04:07 UTC
function checkLength($var){

while (strlen($var) <= 3){

$var = '0' . $var;

}

print $var;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...