Rounding down?

Rounding down?

am 22.08.2009 12:38:26 von Ron Piggott

------=_NextPart_000_0003_01CA22F3.270692E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Is there a way to round down to the nearest 50?

Example: Any number between 400 and 449 I would 400 to be displayed; 450 =
to 499 would be 450; 500 to 549 would be 500, etc?

The original number of subscribers is from a mySQL query and changes =
each day. I am trying to present a factual statement: "There have been =
over ### subscribers in 2009" type of scenereo.

Ron
------=_NextPart_000_0003_01CA22F3.270692E0--

Re: Rounding down?

am 22.08.2009 13:02:37 von David Robley

Ron Piggott wrote:

> Is there a way to round down to the nearest 50?
>
> Example: Any number between 400 and 449 I would 400 to be displayed; 450
> to 499 would be 450; 500 to 549 would be 500, etc?
>
> The original number of subscribers is from a mySQL query and changes each
> day. I am trying to present a factual statement: "There have been over
> ### subscribers in 2009" type of scenereo.
>
> Ron

Modulus - http://php.net/manual/en/language.operators.arithmetic.php



Cheers
--
David Robley

Oxymoron: Split level.
Today is Prickle-Prickle, the 15th day of Bureaucracy in the YOLD 3175.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Rounding down?

am 22.08.2009 14:00:54 von Richard Heyes

Hi,

> Is there a way to round down to the nearest 50?
>
> Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc?

Off the top of my head: divide the number by 50, run floor() on the
result, then times it by 50.

1. 449 / 50 = 9.whatever
2. floor(9.whatever) = 9
3. 9 * 50 = 450

--
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
Lots of PHP and Javascript code - http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Rounding down?

am 22.08.2009 15:02:58 von Ashley Sheridan

On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:
> Hi,
>
> > Is there a way to round down to the nearest 50?
> >
> > Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc?
>
> Off the top of my head: divide the number by 50, run floor() on the
> result, then times it by 50.
>
> 1. 449 / 50 = 9.whatever
> 2. floor(9.whatever) = 9
> 3. 9 * 50 = 450
>
> --
> Richard Heyes
> HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
> Lots of PHP and Javascript code - http://www.phpguru.org
>

It should be round() and not floor().

449 / 50 = 8.98
floor(8.98) = 8
8 * 50 = 400

round(8.98) = 9
9 * 50 = 450



Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Rounding down?

am 22.08.2009 15:11:47 von Ron Piggott

Thanks; Amazing. Ron

----- Original Message -----
From: "Ashley Sheridan"
To: "Richard Heyes"
Cc: "Ron Piggott" ;
Sent: Saturday, August 22, 2009 9:02 AM
Subject: Re: [PHP] Rounding down?


> On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:
>> Hi,
>>
>> > Is there a way to round down to the nearest 50?
>> >
>> > Example: Any number between 400 and 449 I would 400 to be displayed;
>> > 450 to 499 would be 450; 500 to 549 would be 500, etc?
>>
>> Off the top of my head: divide the number by 50, run floor() on the
>> result, then times it by 50.
>>
>> 1. 449 / 50 = 9.whatever
>> 2. floor(9.whatever) = 9
>> 3. 9 * 50 = 450
>>
>> --
>> Richard Heyes
>> HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
>> Lots of PHP and Javascript code - http://www.phpguru.org
>>
>
> It should be round() and not floor().
>
> 449 / 50 = 8.98
> floor(8.98) = 8
> 8 * 50 = 400
>
> round(8.98) = 9
> 9 * 50 = 450
>
>
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>


------------------------------------------------------------ --------------------



No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.409 / Virus Database: 270.13.64/2319 - Release Date: 08/22/09
06:06:00


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Rounding down?

am 22.08.2009 15:15:00 von daniel danon

--0016368e1d58a782900471bac58b
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

I also wrote a function for that,

function round_to($number, $increments) {
$increments = 1 / $increments;
return (round($number * $increments) / $increments);
}

(Also published on php manual - round() )


On Sat, Aug 22, 2009 at 4:11 PM, Ron Piggott wrote:

> Thanks; Amazing. Ron
>
> ----- Original Message ----- From: "Ashley Sheridan" <
> ash@ashleysheridan.co.uk>
> To: "Richard Heyes"
> Cc: "Ron Piggott" ; > >
> Sent: Saturday, August 22, 2009 9:02 AM
> Subject: Re: [PHP] Rounding down?
>
>
> On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:
>>
>>> Hi,
>>>
>>> > Is there a way to round down to the nearest 50?
>>> >
>>> > Example: Any number between 400 and 449 I would 400 to be displayed; >
>>> 450 to 499 would be 450; 500 to 549 would be 500, etc?
>>>
>>> Off the top of my head: divide the number by 50, run floor() on the
>>> result, then times it by 50.
>>>
>>> 1. 449 / 50 = 9.whatever
>>> 2. floor(9.whatever) = 9
>>> 3. 9 * 50 = 450
>>>
>>> --
>>> Richard Heyes
>>> HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
>>> Lots of PHP and Javascript code - http://www.phpguru.org
>>>
>>>
>> It should be round() and not floor().
>>
>> 449 / 50 = 8.98
>> floor(8.98) = 8
>> 8 * 50 = 400
>>
>> round(8.98) = 9
>> 9 * 50 = 450
>>
>>
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
>>
>
>
> ------------------------------------------------------------ --------------------
>
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.409 / Virus Database: 270.13.64/2319 - Release Date: 08/22/09
> 06:06:00
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Use ROT26 for best security

--0016368e1d58a782900471bac58b--

Re: Rounding down?

am 22.08.2009 16:16:41 von Richard Heyes

Hi,

> It should be round() and not floor().
>
> 449 / 50 = 8.98
> floor(8.98) = 8
> 8 * 50 = 400
>
> round(8.98) = 9
> 9 * 50 = 450

Not based on the examples given:

> Example: Any number between 400 and 449 I would 400 to be displayed

--
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
Lots of PHP and Javascript code - http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Rounding down?

am 22.08.2009 18:45:37 von Richard Heyes

Hi,

> ...

A little modification:

/**
* Rounds down to the nearest 50
*/
function myRound($val)
{
$units = intval(substr($val, -2));

return intval(substr($val, 0, -2) . ($units >= 50 ? '50' : '00'));
}

echo myRound(449) . '
'; // 400
echo myRound(450) . '
'; // 450
echo myRound(356) . '
'; // 350
echo myRound(79) . '
'; // 50
?>

PS I haven't checked if there's a PHP function for this.

--
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
Lots of PHP and Javascript code - http://www.phpguru.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Rounding down?

am 23.08.2009 03:06:52 von Clancy

On Sat, 22 Aug 2009 14:02:58 +0100, ash@ashleysheridan.co.uk (Ashley Sheridan) wrote:

>On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:
>> Hi,
>>
>> > Is there a way to round down to the nearest 50?
>> >
>> > Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc?
>>
>> Off the top of my head: divide the number by 50, run floor() on the
>> result, then times it by 50.
>>
>> 1. 449 / 50 = 9.whatever
>> 2. floor(9.whatever) = 9
>> 3. 9 * 50 = 450
>>
>> --
>> Richard Heyes
>> HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
>> Lots of PHP and Javascript code - http://www.phpguru.org
>>
>
>It should be round() and not floor().
>
>449 / 50 = 8.98
>floor(8.98) = 8
>8 * 50 = 400
>
>round(8.98) = 9
>9 * 50 = 450
>
Definitely floor or int, not round.

Round 0.5-1.499 -> 1
Floor 1.0-1.999 -> 1

And if you really want to be accurate you should allow for decimal conversion errors in
any operation involving floating point numbers. Otherwise you cannot rely on (say) 50 not
being represented as 49.9999999999, so that int or floor will give 49. In something like
this I usually add half of the precision I want to work to:

eg: $answer = 50* (int) (($x +0.5)/50);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php