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
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:
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:
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:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 8 years ago.
I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.
You could multiply the number by 10, floor() it, and then divide it back.
echo floor($value * 10) / 10;
Try with this,
echo intval((3.9609053497942*10))/10;
or
echo floor((3.9609053497942*10))/10;
There is so many possible solutions:
echo bcadd(3.9609053497942, 0, 1);
preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
why not treat it as a string, like
$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
Did you tried like:
number_format(3.9609053497942, 1);
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
Lets say I divide 14 / 15 times it by 100 to get the percentage which is 93.33333333333333 how can I display it as 93.3% using php?
Here is the code.
$percent = ($avg / 15) * 100;
The sprintf function is made for this (see also the manual):
echo sprintf('%.1f%%', $percent);
PHP has a number_format function which lets you specify the number of decimals, what to use for the decimal separator, and what to use for the thousands separator:
$percent = ($avg / 15) * 100;
echo number_format($percent, 1) . '%'; // => 93.3%