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.
Related
I am performing subtraction on two variable.
$first_variable = 20;
$second_variable = 30;
$result = $first_variable - $second_variable;
So how do i get that the result $result is positive or negative?
Have any PHP function to determine that the result of subtraction is positive or negative?
I know the i can use if statement to get it done. but i am asking for any predefined function to do it.
The reason i asked it hear is just a curiosity.
You can use php gmp_sign function to achieve that check this
Example:-
<?php
// positive
echo gmp_sign("500") . "\n";
// negative
echo gmp_sign("-500") . "\n";
// zero
echo gmp_sign("0") . "\n";
?>
output
1
-1
0
Well, for pure academic purposes, you can sort of do it with a function:
$sign = sprintf("%+d", $number)[0];
Is there any difference between floor() and intval()? Though both returns the same results, is there any issue with regard to performance? Which of the two is faster? Which is the right php function to use if I just want to return the whole value only of a decimal?
The goal here is to display 1999.99 to 1999, omitting the decimal value and only return the whole number.
$num = 1999.99;
$formattedNum = number_format($num)."<br>";
echo intval($num) . ' intval' . '<br />';
echo floor($num) . ' floor';
The functions give different results with negative fractions.
echo floor(-0.1); // -1
echo intval(-0.1); // 0
The fastest way is to use type casting: (int)$num.
Per the docs,
The return value of floor() is still of type float because the value range of float is usually bigger than that of integer.
Intval() returns an int, however.
$v = 1999.99;
var_dump(
intval($v),
floor($v)
);
Output:
int(1999)
float(1999)
Docs:
http://php.net/manual/en/function.intval.php
http://php.net/manual/en/function.floor.php
$ratio_x = (0.70/100);
$computation = $ratio_x * 8000;
$noDeci = floor(($ratio_x * 8000)*1)/1;
$intval = intval(($ratio_x * 8000)*1)/1;
$noDeci_hc = floor((int)56*1)/1;
echo $computation."|".$noDeci."|".$noDeci_hc."|".$intval;
OUTPUT:
56|55|56|55
$noDeci returns 55???
Hello now my code looks like:
echo number_format($_SESSION['price_summ']."<br>");
But I need to have numbers with decimals... I know it should look something like this:
$row['price'] = intval(($row['price']*100))/100;
But it Does not work.
The simplest way is to store your data in database with type of DECIMAL or FLOAT or DOUBLE. And when you will output data from database it will already be in decimal format. Which type to use is relative. Look threw the web to find optimal solution for your situation.
If you want to use PHP use number_format()
$num = "18";
echo number_format((float)$num, 2, '.', ''); //echo will output 105.00
Its really a lot confusing line
echo number_format($_SESSION['price_summ']."<br>");
I guess you want to convert $_SESSION['price_summ'] to float .
Use floatval() for it.
echo number_format(floatval($_SESSION['price_summ']))."<br>";
Alsi you put the bracket after "<br>" which is wrong. In your second code segment you ate simply doing nothing if you $row['price'] is an integer. If it is an integer, go for this
$row['price'] = intval(($row['price']*100.0))/100;
I'll like to format 1000 to 10.00
The PHP number_format function does not seem to be working for this.
I have tried:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?
Number format will change the "." to a "," but you telling it to format ONE THOUSAND.
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
What you want is simply:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
Is this legit for you ?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
Try this code:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?
Try this: echo number_format($cost,2,'.',',');
EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().
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