PHP intval not acting as expected [duplicate] - php

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm issuing a strange behavior with intval in PHP 7.0
It seems like using intval after a floating number computation returns wrong values.
Here's an example:
echo intval(920); // This prints 920 as expected
echo intval(9.2 * 100); // this prints 919!!!
Probably I'm misunderstanding the correct usage of intval.
Can someone explain me why this happens?

Can you please try this:
(int)(9.2 * 10)
OR
See the example below, this is from a comment in the documentation
$n="19.99";
print intval($n*100); // prints 1998
print intval(strval($n*100)); // prints 1999

Related

How to convert output of date("Ymd") into a number in PHP? [duplicate]

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'));

php Rounding up numbers (3628 > 3630) [duplicate]

This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 6 years ago.
After converting currencies I end up with the numbers below:
3628, 5987 and 2359.
I'd like to round them up so they would appear as 3630, 5990 and 2360.
What is the best approach to accomplish this?
My idea of doing this is adding a 0. in front of the number to achieve 0.3628, then using round(0.3628, 3) so that I would get 0.363 and then finally id have to remove 0. and add another zero at the end to achieve 0.3630.
There must be a better way to do it.
Like so:
echo ceil(5987 / 10) * 10;
outputs 5990

How to do math with a string? [duplicate]

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);

Computing limits in PHP [duplicate]

This question already has answers here:
Is there something that's larger than any numbers in PHP?
(9 answers)
Closed 8 years ago.
I want to compute n^(2/3) as n approaches infinity in PHP.
What I tried:
$n = "INF";
echo pow($n, (2/3));
Desired result:
INF
Actual result:
0
Any suggestions? How to compute limits in PHP?
UPDATE:
OK, first problem solved - I just needed to remove the quotation mark.
But is this actually the way to calculate a limit? Is it interpreted as a limit?
You're passing in the string "INF" into the exponential expression, which isn't valid. Try passing in just POW:
$n = INF;
echo pow($n, (2/3));
// INF

PHP acting weird on my program [duplicate]

This question already has answers here:
int((0.1+0.7)*10) = 7 in several languages. How to prevent this?
(7 answers)
Closed 8 years ago.
Today i started writing a small PHP code and it left me confusing and so i parked here.
<?php
echo (int) ((0.5 + 0.3) * 10); // Outputs 8 as expected
<?php
echo (int) ((0.1 + 0.7) * 10); // Outputs 7 . How ????
Can someone answer with a detailed explanation ?
It is because floating point representation in computers is not exact for some numbers. As already said in the comments, 0.7 is represented internally as 0.699999 or so.
There are two websites that continuously pop up in these kind of questions:
http://floating-point-gui.de/
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
I prefer the first one, as it is a little lighter on the academics. Read that information and you'll understand.

Categories