This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 8 years ago.
Hi am using the following to gen a 4 char numeric
$this->pin = rand(0000,9999)
however if the result is 0435 the leading zero is omitted. How can I keep the leading zero in the result?
You can also:
$number = rand(0,9999);
$this->pin = str_pad($number,4,0,STR_PAD_LEFT);
An integer is not a string, so you can't have a leading zero! :-P
However you can do:
function zfill($str,$n){
$ret = $str;
while(strlen($ret)<$n){
$ret = '0'.$ret;
}
return $ret;
}
$this->pin = zfill($this->pin, 4);
Related
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 2 years ago.
I am getting issue when increasing number.
For example:
$r = 000123 //Integer
Now i want
$r = 000124
I have try with multiple way.. but i didn't get the result. because of starting with 000.
You can do it this way
$num = "00000123";
// get number without zero prefix
preg_match('/(?!0+)\d+/', $num, $match);
// increase by 1
$match[0]++;
// add zero prefix
$newNum = sprintf('%08d', $match[0]);
var_dump($newNum);
What if you try:
$r = 000123
$r++
This question already has answers here:
Add zero to before decimal point of a number
(2 answers)
Closed 2 years ago.
I have some data from database like this :
43966.31875
how to change 43966 to 0 and keep decimal number to 31875, What should I do?
well, for the final result like this :
0.31875
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You
The regex method:
$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);
Will split number into 2 parts then replace the part 1 with "0."
The strstr method:
$string = '43966.31875';
echo '0'.strstr($string, '.');
Also split the number into 2 parts and take the second part and add "0" before
$number = 43966.31875;
$decimalPart = fmod($number, 1);
$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));
Refer the Official Documentation:
PHP fmod
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 3 years ago.
I need a little help with some PHP code. I looked at the examples for doing this with JavaScript and used that as the basis for a PHP implementation. Two issues I haven't be able to resolve. I still get a leading 1 at the start of the string. I also get fewer zeros than specified once the values have trailing zeros (i.e 10, 20, etc).
$num = 2;
$numZeros = 5;
function listRiskNumber($num, $numZeros) {
$n = abs($num);
$zeros = max(0, $numZeros - strlen(floor(json_encode($n))));
$zeroString = substr((pow(10,$zeros)),0,5);
if( $num < 0 ) {
$zeroString = '-' + $zeroString;
}
return $zeroString + $n;
}
$row = listRiskNumber($num, $numZeros);
echo $row;
I want to turn the leading 1 into a zero, and ensure trailing zeros don't get cut off. Any help would be greatly appreciated.
sprintf("%08d", $value);
Solved the problem. Thanks all for the quick answers.
This question already has answers here:
How to remove the leading character from a string?
(10 answers)
Closed 5 years ago.
What is the best way to remove zero before decimal point in php?
These are the two I know of:
ltrim(0.357, '0'); //.357
preg_replace("/0\./i", ".", 0.357); //.357
Are there any better method? Which of them is faster?
You can do it mathematically with floor() function
$n = 0.375;
$whole = floor($n); // 0
$fraction = $n - $whole; // .375
This question already has answers here:
Zero-pad digits in string
(5 answers)
Number with 0 on the front? [closed]
(2 answers)
Closed 8 years ago.
I am trying to extract part of a number using sustr() but the following is not working:
$num = 012014;
echo substr($num, 0,2);
returns 51
BUT
$num = '012014';
echo substr($num, 0,2);
returns 01
I want it to return 01 can someone help me
Is is not a normal number, when it's prepended with a zero (0). Then it's an octal number
If you treat $num as a string, it'll work.
$num = '012014';
echo substr($num, 0,2);
I guess you must declare the variable as string like this.
$num = '012014';
echo substr( (string)$num, 0, 2 );
Ooops I didn't notice the leading zero. You should just define $num as a string
$num = '0123';
Please try this
$num = 123424;
$num = (string)$num;
echo substr($num, 0,2);