I aim to remove leading zeros like this:
echo ltrim(000.1, '0'); // .1 (should end up as 0.1)
echo ltrim(0, '0'); // empty (should end up as 0)
echo ltrim(00005.5, '0'); // 5.5 (correct)
Using ltrim() works fine with values like 00005.5 but doesn't work with 0.1 as 0 (as you would expect by the logic).
My question is, how can I remove leading zeros in values like 0.5 and avoid trimming value if it is 0?
Just multiply it with 1 and php will cast it to float.
echo "000.1"*1 . "\n"; //0.1
echo "0"*1 . "\n"; //0
echo "00005.5"*1 . "\n";//5.5
https://3v4l.org/mnN56
Or float cast it
echo (float)"000.1" . "\n";
echo (float)"0" . "\n";
echo (float)"00005.5" . "\n";
Related
On flipping/inverting bits in PHP using the ~-operator the output contains a lot of leading signs, which are obviously unnecessary, if you output the raw integer. On parsing the binary-value back to an integer, you will see that the values are not equal. See the example attached below.
$integer = 0b11110; // raw integer
echo "{$integer}: " . decbin($integer) . PHP_EOL; // print integer-value before flipping
$integer = ~$integer; // flipping/inverting the integer
$binary = decbin($integer);
echo "{$integer}: " . $binary . PHP_EOL; // print integer-value after flipping
echo bindec($binary) . PHP_EOL; // print parsed binary-value
see result on 3v4l.org
Is it possible to remove the leading signs, so that the second usage of echo will output something like 100001 (or just 00001, if you don't take the plus/minus sign into account), because -31 is obviously not the same as 1111111111111111111111111111111111111111111111111111111111100001.
I've tried casting to float and number_format but float will always round at two and number_format is fixed on the amount of decimals you specify.
So how can I do this like the following conversion
11.2200 -> 11.22
11.2000 -> 11.20
11.2340 -> 11.234
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
This might help, You can use sprintf given by PHP.
You can use float casting
echo (float) 11.2200;
echo "<br/>";
echo (float) 11.2000;
echo "<br/>";
echo (float) 11.2340;
and you have to check number of digits after decimal point and than get value like below :
$val=(float) 11.2000;
if(strlen(substr(strrchr($val, "."), 1))<2){
echo number_format($val,2);
}
You may use the round() function for this.
i-e round(number,precision,mode);
Example:
echo(round(11.2200,2));
Output
11.22
Thanks
Not sure if you need a fix for this anymore, but I just ran into the same problem and here's my solution:
$array = array(11.2200, 11.2000, 11.2340);
foreach($array as $x)
{
// CAST THE PRICE TO A FLOAT TO GET RID OF THE TRAILING ZEROS
$x = (float)$x
// EXPLODE THE PRICE ON THE DECIMAL (IF IT EXISTS)
$pieces = explode('.',$x);
// IF A SECOND PIECE EXISTS, THAT MEANS THE FLOAT HAS AT LEAST ONE DECIMAL PLACE
if(isset($pieces[1]))
{
// IF THE SECOND PIECE ONLY HAS ONE DIGIT, ADD A TRAILING ZERO TO FORMAT THE CURRENCY
if(strlen($pieces[1]) == 1)
{
$x .= '0';
}
}
// IF NO SECOND PIECE EXISTS, ADD A .00 TO IT TO FORMAT THE CURRENCY VALUE
else
{
$x .= '.00';
}
}
i am working on probabilities i need to convert the fractional number into decimal number. as i used the '/' in my query for finding the probabilities.
code for getting the probabilities
<?php
$sql1=mysql_query("SELECT Deprication,
CONCAT(
SUM(CASE WHEN `number of room` = '$pane1' THEN 1 ELSE 0 END),
'/',
COUNT(Deprication)) AS `Probability of yes`
FROM expert
GROUP BY Deprication");
$col1=array();
while($u6=mysql_fetch_array($sql1))
{
echo "<tr>";
echo "<td></br>" . $u6['Deprication'] . "</td></br>";
echo "<td></br>" . $u6['Probability of yes'] . "</td></br>";
$col1[]=$u6['Probability of yes'];
}
now i want to convert the 1/5,1/4,1/2 into decimal.
output
no 1/4
yes 2/6
A dirty solution can be like this. I'm not sure if there's any smarter solution.
<?PHP
$s = "2/3";
$a = explode("/",$s);
echo $a[0]/$a[1];
?>
In your case,
<?PHP
$a = explode("/",$u6['Probability of yes']);
$v = $a[0]/$a[1];
array_push($col1,$v);
?>
You should select the SUM and COUNT separately instead of selecting the concatenated result. Then you'll be able to take ($row['sum'] / $row['count']) in your code or convert it as needed.
I got some PHP code here:
<?php
echo 'hello ' . 1 + 2 . '34';
?>
which outputs 234,
But when I add a number 11 before "hello":
<?php
echo '11hello ' . 1 + 2 . '34';
?>
It outputs 1334 rather than 245 (which I expected it to). Why is that?
That's strange...
But
<?php
echo '11hello ' . (1 + 2) . '34';
?>
or
<?php
echo '11hello ', 1 + 2, '34';
?>
fixes the issue.
UPDATE v1:
I finally managed to get the proper answer:
'hello' = 0 (contains no leading digits, so PHP assumes it is zero).
So 'hello' . 1 + 2 simplifies to 'hello1' + 2 is 2. Because there aren't any leading digits in 'hello1' it is zero too.
'11hello ' = 11 (contains leading digits, so PHP assumes it is eleven).
So '11hello ' . 1 + 2 simplifies to '11hello 1' + 2 as 11 + 2 is 13.
UPDATE v2:
From Strings:
The value is given by the initial portion of the string. If the string
starts with valid numeric data, this will be the value used.
Otherwise, the value will be 0 (zero). Valid numeric data is an
optional sign, followed by one or more digits (optionally containing a
decimal point), followed by an optional exponent. The exponent is an
'e' or 'E' followed by one or more digits.
The dot operator has the same precedence as + and -, which can yield unexpected results.
That technically answers your question... if you want numbers to be treated as numbers during concatenation, just wrap them in parentheses.
<?php
echo '11hello ' . (1 + 2) . '34';
?>
You have to use () in a mathematical operation:
echo 'hello ' . (1 + 2) . '34'; // output hello334
echo '11hello ' . (1 + 2) . '34'; // output 11hello334
You should check the PHP type conversion table to get a better idea of what's happening behind the scenes.
If you hate putting operators in between, assign them to a variable:
$var = 1 + 2;
echo 'hello ' . $var . '34';
How can I get php to not use 1.297503E+17 on large int but 129750300000000000
code:
$dag = 29;
$maand = 03;
$jaar = 2012;
$expdate = $dag . "-" . $maand . "-" . $jaar;
$unixstamp = strtotime($expdate);
echo $unixstamp."<br />";
$winstamp = ($unixstamp + 11644560000) * 10000000;
I'm trying to use the number for a Timestamp in ldap.
That's what I would do (tested on 32b platform)
>> number_format(1.297503E+17,0,'.','')
'129750300000000000'
just be aware, that what you get back is a string, an will be converted back to float if you try doing any arithemtics on it. If you need to do math on large integers look into bc_math extension
PHP internally uses big enough integers. Your problem here is the use of echo:
printf ("%d", $winstamp);
$winstamp++;
printf ("%d", $winstamp);
output:
129775320000000000
129775320000000001
Hope this helps
echo rtrim(sprintf("%0.15f", $winstamp), "0.");
This uses sprintf to print a maximum of 15 decimal places, and then trims off any trailing 0 or . chars. (Of course, there's no guarantee that everything will be rounded nicely with trailing zeros as you might expect.)
If you just want a fixed size, then you can adjust the 15 and remove the rtrim.
Apparently, when PHP encounters a number that exceeds the upper limit of 2,147,483,647 for an integer, it automatically converts the number’s type from integer into a double.
Fortunately, we can format these numbers in scientific notation back to their standard integer form using the number_format() function. Here is how to do it:
$winstamp = 1202400000;
$formatted_stamp = number_format($winstamp , 0, '.', '');
echo $formatted_stamp; //outputs 1202400000 as expected