472,958 Members | 2,017 Online

# round number to the nearest...

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.

Aug 27 '07 #1
6 3151 <comp.lang.php>
<>
<Mon, 27 Aug 2007 14:36:14 -0700>
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>";

}

?>
--
(c) The Amazing Krustov
Aug 27 '07 #2
dk*******@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_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
Aug 27 '07 #3
Krustov wrote:
<comp.lang.php>
<>
<Mon, 27 Aug 2007 14:36:14 -0700>
>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.
js*******@attglobal.net
==================
Aug 28 '07 #4
On Aug 27, 5:59 pm, Krustov <m...@privacy.netwrote:
<comp.lang.php>
<>
<Mon, 27 Aug 2007 14:36:14 -0700>
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>";

}

?>

--
(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....

Aug 28 '07 #5

"Krustov" <me@privacy.netwrote in message
| <comp.lang.php>
| <>
| <Mon, 27 Aug 2007 14:36:14 -0700>
|
| 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==".")
| {

which assumes that a dot will always denote cents, which doesn't make sense.
;^)
Aug 28 '07 #6
<comp.lang.php>
<>
<Mon, 27 Aug 2007 18:57:37 -0700>
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>";

}

?>

--
(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 <br><br>";
}

if (\$poop=="5")
{
\$albundy=\$snowwhite . \$poop;
print "albundy = \$albundy <br><br>";
}

if (\$poop=="plop")
{
\$albundy=\$snowwhite . "0";
\$albundy=\$albundy+0.10;
print "albundy = \$albundy <br><br>";
}

Havent tested the +0.10 and it probably wont work .
--
(c) The Amazing Krustov
Aug 28 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 4 by: tertius | last post by: Hi, I'm trying to round my float total to the nearest .05 cents. 12.01 should produce 12.00 0.14 should produce 0.10 2.28 " 2.25 703.81 " 703.80 ... 6 by: Penguin | last post by: At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double... 10 by: tmeister | last post by: I'm trying to determine the best approach for rounding in an application I'm building. Unfortunately it appears as though SQL Server and VB.NET round in different ways. SQL Server select... 9 by: Ronald W. Roberts | last post by: I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something... 4 by: Chris Davoli | last post by: The folllowing will round to 526, but it should round to 527. It works correctly for all other numbers, except for this one. Does anybody know of a bug in Math.Round? Dim ldecWater As Decimal =... 0 by: thaisummitneel | last post by: sir I have created a database in ms-access.Connected database using ADODB I have written a query to round integer to nearest value such as con.execute"update tablename set... 4 by: Jassim Rahma | last post by: I have a number, for example 0.152 or 1.729 and I want to allow to round to the first 0.010 or 0.050 or 0.100 7 by: Will Rocisky | last post by: I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70. How can I achieve that? 0 by: Edwin.Madari | last post by: >>round(76.1, -2) 100.0 80.0 76.0 builtin function round, will work for you...... Help on built-in function round in module __builtin__: round(...) round(number) -floating point number 0 by: lllomh | last post by: Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{ 2 by: DJRhino | last post by: Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 0 by: Aliciasmith | last post by: In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app... 2 by: giovanniandrean | last post by: The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions... 1 by: Teri B | last post by: Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and... 3 by: nia12 | last post by: Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of... 0 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike... 2 by: GKJR | last post by: Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...