This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 7 years ago.
So lets show an example here shall we?
Let's say I have 2 strings that are like this....
$math1 = "+300";
$math2 = "-125";
So obviously the answer would have to be +175 but how do I actually do the math while the input is a string.
I can't simply do
$math1 - $math2
Because it'd have to figure out if it is a + or a -, so how can this exactly be done?
You can use intval to get the integer value of a variable, and then you can do basic math operations.
<?php
$math1 = intval("+300");
$math2 = intval("-125");
echo $math1 - $math2;
?>
intval($math1) + intval($math2);
Can be:
$result = abs($math1) - abs($math2);
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I Have a string like this
{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}-
I need the value of Balance.
I tried like this.
$string = '{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":1,"Code":1,"Message":"Success"}-';
$withCharacter = strstr($string, 'Balance":');
echo substr($withCharacter, 1);
Tried to use explode also but no luck.
This seems like a valid JSON, why not json_decode and find the value:
$i = json_decode('{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}');
echo $i->Account->Balance;
This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 2 years ago.
I have a value like 12,25,246
I want to get 1225246 (must be an integer)
How can I do that?
I have searched a lot but there are direct answer like converting string to integer but not like this actually these both are like integer
I have tried php formate_number but it did not worked.
You could use a combination of intval() and str_replace() to do this.
Example:
$value = '12,25,246';
var_dump(intval(str_replace(',','',$value)));
// Yields: "int(1225246)"
Sandbox
$number = (int)str_replace(',', '', '12,25,246');
here is it
This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 5 years ago.
I want to have the desired output as such 20170613 which is an integer.
I know using strtotime() I can get an UNIX timestamp as an integer, but I don't want that. date("Ymd") however returns a string.
I can't seem to figure a way to convert this to an integer.
Edit #1: Here is what I am attempting:
$x = (int)date("Ymd");
echo $x;
The result however does not show up in the browser. Infact in the developer's tools, it shows internal server error.
The term to Google is "type cast". That leads you to the PHP type juggling docs on integer casting.
Taking that as a reference point, the canonical way to go about it is:
$int = (int)date('Ymd');
For completeness, you could also use the equivalent full form:
$int = (integer)date('Ymd');
Or the functional:
$int = intval(date('Ymd'));
This question already has answers here:
How to make numbers not be shown in scientific form?
(2 answers)
Closed 9 years ago.
I got a Variable $a as result from calculation from equations.
$a = 2.367760572051E-5
I need an output so the result of $a is 0.00002367760572051
So I try to convert the variable to double using this :
$result = (double)$a;
But the result is still the same,.
Is there any way to convert it?
If all you're after is a formatted string, try this one out
$result = sprintf('%0.17f', $a);
There's also
$result = number_format($a, 17);
if you want a result to print, try this.
$str = sprintf("%.20f",$a);
echo $str;
then you can result belows as.
0.00002367760572051
PHP is dynamically typed. Your variable $a is already a double internally. No need to typecast it again.
Use function sprintf to format the double as desired.
This question already has answers here:
How to evaluate formula passed as string in PHP?
(1 answer)
PHP: Calculate a math function f(x) in a string
(1 answer)
Closed 9 years ago.
I need to convert the formula like string to its calculated value, in PHP.
For eg:
$str = "(20000+(2000*(40/100)))*(10/100)";
$calculated_value = ????; // Calculated value of the above string "$str".
I need to calculate value of the string "$str" and store it in separate string.
Please help me out.
Thanks in advance.
Take a look on this: http://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html
And Process mathematical equations in php