RE: [PHP] How do i assign Integers only and not real numbers?

From: Date: Mon, 23 Sep 2002 16:06:14 +0000
Subject: RE: [PHP] How do i assign Integers only and not real numbers?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-117298@lists.php.net to get a copy of this message
Thanks for the answers all, I`ve combined the floor and the precision answer and it does exactly what I want it to do. Cheers, Tom -----Original Message----- From: 1LT John W. Holmes [mailto:holmes072000@charter.net] Sent: 23 September 2002 16:24 To: Tom; php-general@lists.php.net; Support @ Fourthrealm.com Subject: Re: [PHP] How do i assign Integers only and not real numbers? 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 > - - - - - - - - - - - - - - - - - - - - -

« previous php.general (#117298) next »