![]() |
Floating point to integer?
For example, if the value of a integer called number is 4856756.55, it should be converted to 4856757. I thought there was a default function by PHP for that but I couldn't find any function. Type juggling doesn't work!!! ARGH!!! *!*
|
When converting from a float to an int, PHP just strips the decimal point and everything after it. For example:
PHP Code:
For more precise numbers, you'll need to use the BCMath extension in PHP: PHP: BC math - Manual Alan |
Err... round() works too. You can even specify the precision.
PHP Code:
|
My thought exactly. You can also simply explode on the dot and then print the $float[1]... ;-)
|
I think my memory is going, using round() didn't even occur to me, was just thinking of typecasting. Must be getting old :-(
Alan. |
Quote:
|
...And floor() !
|
Doesn't intval() just do the same as typecasting to int and always round down?
Alan |
Correct. ;-)
PHP Code:
Code:
(like 26.98 - 13 = -13.98 => -13) |
intval doesn't round anything. it just returns the integer part of the number. number_format rounds the number automatically (depending on the decimal precision).
PHP Code:
|
Actually, that isn't rounding down is -13.98 yields 13, that's just dropping the decimal value. -13.98 rounds down to -14, not -13. Technically that should be a call to floor(), but the results are disparate.
|
| All times are GMT. The time now is 09:55 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0