round number to the nearest...

round number to the nearest...

am 27.08.2007 23:36:14 von dkirkdrei

I am looking for a way to round a number (which will be displayed as a
dollar amount) to the nearest nickel. Using the php round function
only allows you to control the number of decimal places in a number. I
need to round to the nearest .05. For example: 5.36 would be rounded
to 5.35, 10.49 would be rounded to 10.50, etc.. Any help would be
greatly appreciated.

Re: round number to the nearest...

am 27.08.2007 23:59:17 von Hans-Peter Sauer


<>

<1188250574.997233.228620@57g2000hsv.googlegroups.com>

> I am looking for a way to round a number (which will be displayed as a
> dollar amount) to the nearest nickel. Using the php round function
> only allows you to control the number of decimal places in a number. I
> need to round to the nearest .05. For example: 5.36 would be rounded
> to 5.35, 10.49 would be rounded to 10.50, etc.. Any help would be
> greatly appreciated.
>

Here goes the worst ever php code posted to this newsgroup .

But hang loose as somebody will be along shortly with a one line
solution :-)



$demo=12.34;

print "demo = $demo

";

$daffyduck=strlen($demo);
print "daffyduck = $daffyduck

";

$mickymouse=substr($demo,$daffyduck-3,1);
print "mickymouse = $mickymouse

";

if ($mickymouse==".")
{

$snowwhite=substr($demo,0,$daffyduck-1);
print "snowwhite = $snowwhite

";

$dwarfs=substr($demo,$daffyduck-1,1);
print "dwarfs = $dwarfs

";

if ($dwarfs==0) {$poop=0;}
if ($dwarfs==1) {$poop=0;}
if ($dwarfs==2) {$poop=0;}
if ($dwarfs==3) {$poop=5;}
if ($dwarfs==4) {$poop=5;}
if ($dwarfs==5) {$poop=5;}
if ($dwarfs==6) {$poop=5;}
if ($dwarfs==7) {$poop=0;}
if ($dwarfs==8) {$poop=0;}
if ($dwarfs==9) {$poop=0;}

$albundy=$snowwhite . $poop;
print "albundy = $albundy

";

}

?>


--
(c) The Amazing Krustov

Re: round number to the nearest...

am 28.08.2007 00:05:06 von ivansanchez-alg

dkirkdrei@yahoo.com wrote:

> I am looking for a way to round a number (which will be displayed as a
> dollar amount) to the nearest nickel. Using the php round function
> only allows you to control the number of decimal places in a number. I
> need to round to the nearest .05. For example: 5.36 would be rounded
> to 5.35, 10.49 would be rounded to 10.50, etc.. Any help would be
> greatly appreciated.

Multiply by 20, then round, then divide by 20. That should do it.

function round_to_nickel($number)
{
return ( round( $number*20 ) /20);
}

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net

Re: round number to the nearest...

am 28.08.2007 02:33:04 von Jerry Stuckle

Krustov wrote:
>
> <>
>
> <1188250574.997233.228620@57g2000hsv.googlegroups.com>
>
>> I am looking for a way to round a number (which will be displayed as a
>> dollar amount) to the nearest nickel. Using the php round function
>> only allows you to control the number of decimal places in a number. I
>> need to round to the nearest .05. For example: 5.36 would be rounded
>> to 5.35, 10.49 would be rounded to 10.50, etc.. Any help would be
>> greatly appreciated.
>>
>
> Here goes the worst ever php code posted to this newsgroup .
>
> But hang loose as somebody will be along shortly with a one line
> solution :-)
>
>
> >
> $demo=12.34;
>
> print "demo = $demo

";
>
> $daffyduck=strlen($demo);
> print "daffyduck = $daffyduck

";
>
> $mickymouse=substr($demo,$daffyduck-3,1);
> print "mickymouse = $mickymouse

";
>
> if ($mickymouse==".")
> {
>
> $snowwhite=substr($demo,0,$daffyduck-1);
> print "snowwhite = $snowwhite

";
>
> $dwarfs=substr($demo,$daffyduck-1,1);
> print "dwarfs = $dwarfs

";
>
> if ($dwarfs==0) {$poop=0;}
> if ($dwarfs==1) {$poop=0;}
> if ($dwarfs==2) {$poop=0;}
> if ($dwarfs==3) {$poop=5;}
> if ($dwarfs==4) {$poop=5;}
> if ($dwarfs==5) {$poop=5;}
> if ($dwarfs==6) {$poop=5;}
> if ($dwarfs==7) {$poop=0;}
> if ($dwarfs==8) {$poop=0;}
> if ($dwarfs==9) {$poop=0;}
>
> $albundy=$snowwhite . $poop;
> print "albundy = $albundy

";
>
> }
>
> ?>
>
>

I do admit - your solution is "ducky"! - and it "dwarfs" the ideas I
came up with.

Of course, this is a pure snow white PHP solution!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: round number to the nearest...

am 28.08.2007 03:57:37 von dkirkdrei

On Aug 27, 5:59 pm, Krustov wrote:
>
> <>
>
> <1188250574.997233.228...@57g2000hsv.googlegroups.com>
>
> > I am looking for a way to round a number (which will be displayed as a
> > dollar amount) to the nearest nickel. Using the php round function
> > only allows you to control the number of decimal places in a number. I
> > need to round to the nearest .05. For example: 5.36 would be rounded
> > to 5.35, 10.49 would be rounded to 10.50, etc.. Any help would be
> > greatly appreciated.
>
> Here goes the worst ever php code posted to this newsgroup .
>
> But hang loose as somebody will be along shortly with a one line
> solution :-)
>
> >
> $demo=12.34;
>
> print "demo = $demo

";
>
> $daffyduck=strlen($demo);
> print "daffyduck = $daffyduck

";
>
> $mickymouse=substr($demo,$daffyduck-3,1);
> print "mickymouse = $mickymouse

";
>
> if ($mickymouse==".")
> {
>
> $snowwhite=substr($demo,0,$daffyduck-1);
> print "snowwhite = $snowwhite

";
>
> $dwarfs=substr($demo,$daffyduck-1,1);
> print "dwarfs = $dwarfs

";
>
> if ($dwarfs==0) {$poop=0;}
> if ($dwarfs==1) {$poop=0;}
> if ($dwarfs==2) {$poop=0;}
> if ($dwarfs==3) {$poop=5;}
> if ($dwarfs==4) {$poop=5;}
> if ($dwarfs==5) {$poop=5;}
> if ($dwarfs==6) {$poop=5;}
> if ($dwarfs==7) {$poop=0;}
> if ($dwarfs==8) {$poop=0;}
> if ($dwarfs==9) {$poop=0;}
>
> $albundy=$snowwhite . $poop;
> print "albundy = $albundy

";
>
> }
>
> ?>
>
> --
> (c) The Amazing Krustov

Krustov,

I am loving the script but I think I will have to work on the $poop a
bit. lol, When I get to a situation where the 1st digit after the
decimal needs to be rounded up, it still rounds down. This adds even
more diffuculty to it when I have a number like 5.98 as the entire
number will round to 6.00. Thanks for the help!

dkirk....

Re: round number to the nearest...

am 28.08.2007 15:48:47 von Steve

"Krustov" wrote in message
news:MPG.213d59927dbc988b98ac29@news.newsreader.com...
|
| <>
|
| <1188250574.997233.228620@57g2000hsv.googlegroups.com>
|
| > I am looking for a way to round a number (which will be displayed as a
| > dollar amount) to the nearest nickel. Using the php round function
| > only allows you to control the number of decimal places in a number. I
| > need to round to the nearest .05. For example: 5.36 would be rounded
| > to 5.35, 10.49 would be rounded to 10.50, etc.. Any help would be
| > greatly appreciated.
| >
|
| Here goes the worst ever php code posted to this newsgroup .
|
| But hang loose as somebody will be along shortly with a one line
| solution :-)
|
|
| |
| $demo=12.34;
|
| print "demo = $demo

";
|
| $daffyduck=strlen($demo);
| print "daffyduck = $daffyduck

";
|
| $mickymouse=substr($demo,$daffyduck-3,1);
| print "mickymouse = $mickymouse

";
|
| if ($mickymouse==".")
| {

which assumes that a dot will always denote cents, which doesn't make sense.
;^)

Re: round number to the nearest...

am 29.08.2007 00:54:49 von Hans-Peter Sauer


<>

<1188266257.277944.233110@o80g2000hse.googlegroups.com>

> > if ($dwarfs==0) {$poop=0;}
> > if ($dwarfs==1) {$poop=0;}
> > if ($dwarfs==2) {$poop=0;}
> > if ($dwarfs==3) {$poop=5;}
> > if ($dwarfs==4) {$poop=5;}
> > if ($dwarfs==5) {$poop=5;}
> > if ($dwarfs==6) {$poop=5;}
> > if ($dwarfs==7) {$poop=0;}
> > if ($dwarfs==8) {$poop=0;}
> > if ($dwarfs==9) {$poop=0;}
> >
> > $albundy=$snowwhite . $poop;
> > print "albundy = $albundy

";
> >
> > }
> >
> > ?>
> >
> > --
> > (c) The Amazing Krustov
>
> Krustov,
>
> I am loving the script but I think I will have to work on the $poop a
> bit. lol, When I get to a situation where the 1st digit after the
> decimal needs to be rounded up, it still rounds down. This adds even
> more diffuculty to it when I have a number like 5.98 as the entire
> number will round to 6.00. Thanks for the help!
>

Dont you mean it will round down to 5.90 ? .


if ($dwarfs==7) {$poop="plop";}
if ($dwarfs==8) {$poop="plop";}
if ($dwarfs==9) {$poop="plop";}

if ($poop=="0")
{
$albundy=$snowwhite . $poop;
print "albundy = $albundy

";
}

if ($poop=="5")
{
$albundy=$snowwhite . $poop;
print "albundy = $albundy

";
}

if ($poop=="plop")
{
$albundy=$snowwhite . "0";
$albundy=$albundy+0.10;
print "albundy = $albundy

";
}

Havent tested the +0.10 and it probably wont work .


--
(c) The Amazing Krustov