Michelle Konzack
[PHP] Conversion Float => Integer
September 16, 2017 03:40PM
What is the easiest method to convert a Float to Integer?

I have (only) to get rid of anything after the decimal point.

Thanks

--
Michelle Konzack Miila ITSystems @ TDnet
GNU/Linux Developer 00372-54541400
AshleySheridan
Re: [PHP] Conversion Float => Integer
September 16, 2017 03:50PM
On Sat, 2017-09-16 at 16:36 +0300, Michelle Konzack wrote:
> What is the easiest method to convert a Float to Integer?
>
> I have (only) to get rid of anything after the decimal point.
>
> Thanks
>
>

There are a few ways:

$int = intval($float);
$int = (int)$float;
$int = floor($float);

Not sure which is faster, but they're all pretty easy
-- 
Thanks,
Ash

http://www.ashleysheridan.co.uk
Yuriy Peskov
Re: [PHP] Conversion Float => Integer
September 16, 2017 03:50PM
I think (int)$float is the fastest as it is not a function call.

In fact the difference is a really small one.



On 16.09.17 16:40, AshleySheridan wrote:
> On Sat, 2017-09-16 at 16:36 +0300, Michelle Konzack wrote:
>> What is the easiest method to convert a Float to Integer?
>>
>> I have (only) to get rid of anything after the decimal point.
>>
>> Thanks
>>
>>
> There are a few ways:
>
> $int = intval($float);
> $int = (int)$float;
> $int = floor($float);
>
> Not sure which is faster, but they're all pretty easy
> --
> Thanks,
> Ash
>
> http://www.ashleysheridan.co.uk
>
>

--
Best regards,
Yuriy Peskov


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Michelle Konzack
Re: [PHP] Conversion Float => Integer
September 16, 2017 06:30PM
Hello Ashley,

On 2017-09-16 14:40:16 AshleySheridan hacked into the keyboard:
> There are a few ways:
>
> $int = intval($float);

Argh! -- This was exactly what I was looking for!

Had a provisory solution in for of a stringfunction which looks
absolutely courious :<D but it worked!!!

Normally I should not show it, but here it is:

$AVE = $TOT / date('t', strtotime($date)) . '.';
$POS = strpos($AVE, '.');
$LEN = strlen($AVE);
$LENGHT = $LEN - $POS;
$AVE = substr_replace($AVE, '', $POS, $LENGHT);

It is a KILLER I know!

Have a nice evening!

--
Michelle Konzack Miila ITSystems @ TDnet
GNU/Linux Developer 00372-54541400
Christoph M. Becker
Re: [PHP] Conversion Float => Integer
September 16, 2017 11:40PM
On 16.09.2017 at 15:40, AshleySheridan wrote:

> On Sat, 2017-09-16 at 16:36 +0300, Michelle Konzack wrote:
>
>> What is the easiest method to convert a Float to Integer?
>>
>> I have (only) to get rid of anything after the decimal point.
>
> There are a few ways:
>
> $int = intval($float);
> $int = (int)$float;
> $int = floor($float);

It might be appropriate to point out that the latter has different
results than the former two, if $float is a negative non-integral value,
and that floor() returns a float.

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login