You are here: Re: round number to the nearest... « PHP Programming Language « IT news, forums, messages
Re: round number to the nearest...

Posted by Jerry Stuckle on 08/28/07 00:33

Krustov wrote:
> <comp.lang.php>
> <>
> <Mon, 27 Aug 2007 14:36:14 -0700>
> <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 :-)
>
>
> <?php
>
> $demo=12.34;
>
> print "demo = $demo <br><br>";
>
> $daffyduck=strlen($demo);
> print "daffyduck = $daffyduck <br><br>";
>
> $mickymouse=substr($demo,$daffyduck-3,1);
> print "mickymouse = $mickymouse <br><br>";
>
> if ($mickymouse==".")
> {
>
> $snowwhite=substr($demo,0,$daffyduck-1);
> print "snowwhite = $snowwhite <br><br>";
>
> $dwarfs=substr($demo,$daffyduck-1,1);
> print "dwarfs = $dwarfs <br><br>";
>
> 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 <br><br>";
>
> }
>
> ?>
>
>

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
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация