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.
Related
This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);
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:
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:
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);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP negatives keep adding
I have this code here....
$remaining = 0;
foreach($array as $value=>$row){
$remaining = $remaining + $row['remainingbalance'];
}
What its doing is that it is going through all the remaining balances in the array which are -51.75 and -17.85 with the code above I get -69.60 which is correct. But I am wondering how when its two negatives if they could subtract? Is that possible?
I tried this
$remaining = 0;
foreach($clientArrayInvoice as $value=>$row){
$remaining = $remaining + abs($row['remainingbalance']);
}
but it gives me 69.60 without the negative.
Anyone got any ideas?
my goal is to take -51.75 and -17.85 and come up with -33.90 only when its a negative to do subtract. otherwise add
Whenever you add a negative number, you actually subtract the positive value (and the other way around).
So 0 + (-16) = 0 - 16 = -16.
When you call abs() you calculate something completely different.