Re: How do i assign Integers only and not real numbers?
| From: | 1LT John W. Holmes | Date: | Mon, 23 Sep 2002 15:24:14 +0000 |
| Subject: | Re: How do i assign Integers only and not real numbers? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-117291@lists.php.net to get a copy of this message | ||
If you end up using floor(), watch for floating point errors. You could
expect your division to come out to a whole number, say 12, but be
represented by 11.99999999999 in the computer. So that'll floor() to 11. You
can add a fluff to your division to counteract this:
$years = floor($years + ($themonth / 12) + 0.0001);
---John Holmes...
----- Original Message -----
From: "Support @ Fourthrealm.com" <support@fourthrealm.com>
To: "Tom" <tom.callaghan@claimnet.co.uk>; <php-general@lists.php.net>
Sent: Monday, September 23, 2002 11:06 AM
Subject: Re: [PHP] How do i assign Integers only and not real numbers?
> Hi Tom,
> Try this:
> $years = round($years + ( $themonth / 12 ));
>
>
> From the manual:
> float round ( float val [, int precision])
> Returns the rounded value of val to specified precision (number of digits
> after the decimal point). precision can also be negative or zero
(default).
>
>
> ceil() and floor() also exist.
>
>
>
> Peter
>
>
> At 03:46 PM 9/23/2002 +0100, Tom wrote:
> >Hi all,
> >
> >I have a line of code that assigns a new year number: -
> >
> >$years = $years + ( $themonth / 12 );
> >
> >but sometimes $years == 1998.0833333333
> >
> >or
> >
> >$year == 2002.75
> >
> >etc...
> >
> >I cant find anything like a round() or floor() function in PHP so that
year
> >would be 1998 or 2002 only.
> >
> >Does anyone know what function would do this? Sorry if I`m wasting your
> >time if its a really obvious one!
> >
> >thanks in advance,
> >Tom
> >
> >
> >
> >--
> >PHP General Mailing List (http://www.php.net/)
> >To unsubscribe, visit: http://www.php.net/unsub.php
>
> - - - - - - - - - - - - - - - - - - - - -
> Fourth Realm Solutions
> pbechard@fourthrealm.com
> http://www.fourthrealm.com
> Tel: 519-739-1652
> - - - - - - - - - - - - - - - - - - - - -