473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Round and Ceil problem

I have a problem that I can't seem to solve. I have checked the round,
ceil and floor functions and they don't seem to do what I want.

I have an entry like this

<?php
$tax = .065;
$ad2day = 104.50;

$ad2day = round(($ad2day * $tax)+ $ad2day, 2);
echo printf ("%.2f", $ad2day);
?>

That outputs $111.29, the result of multiplying 104.50x.065 with the
answer rounded to 2 decimal places, then adding in the 104.50 of the
$ad1day variable.

What I want to do is multiply the $ad2day variable by the $tax and come
up with a result that will round up any remainder past two decimal
places to the next decmial place. In the example above, the ($ad2day *
$tax) comes out to 6.7925. I want to round that up to 2 decimal places
to 6.80 if there is *any* remainder past the two digits. So 6.2401 would
be 6.25, 6.2499 would be 6.25, etc.

Round doesn't seem to do it and ceil only works (i think anyway)
rounding up to whole numbers. What do I need to do to get the result the
way I want it?

Thanks
Jul 17 '05 #1
4 4383
Jack wrote:
I have checked the round,
ceil and floor functions and they don't seem to do what I want. [...] So 6.2401 would be 6.25, 6.2499 would be 6.25, etc.

What do I need to do to get the result the
way I want it?


$x = 6.2501;
$y = ceil($x * 100) / 100; // 6.26

--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #2
Pedro Graca wrote:
Jack wrote:

[...]
So 6.2401 would be 6.25, 6.2499 would be 6.25, etc.

What do I need to do to get the result the
way I want it?

$x = 6.2501;
$y = ceil($x * 100) / 100; // 6.26

Okay thanks, but that assumes that I already know the value of $x when
in fact I won't. How can I incorporate that into my original statement of
$ad2day = round(($ad2day * $tax)+ $ad2day, 2);

I really don't want to write a function for each of my 150 entries.
Jul 17 '05 #3
JackM wrote:
Pedro Graca wrote:
Jack wrote:

[...]
So 6.2401 would be 6.25, 6.2499 would be 6.25, etc.

What do I need to do to get the result the
way I want it?

$x = 6.2501;
$y = ceil($x * 100) / 100; // 6.26

How can I incorporate that into my original statement of
$ad2day = round(($ad2day * $tax)+ $ad2day, 2);


$x = $ad2day * $tax;
$y = ceil($x * 100) / 100;
$ad2day = round($y + $ad2day, 2);
Replacing $y:

$x = $ad2day * $tax;
$ad2day = round((ceil($x * 100) / 100) + $ad2day, 2);
/* $y = ceil($x * 100) / 100; */

Replacing $x

$ad2day = round((ceil(($ad2day * $tax) * 100) / 100) + $ad2day, 2);
/* $x = $ad2day * $tax; */

--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #4
Pedro Graca wrote:
JackM wrote:
Pedro Graca wrote:


$x = 6.2501;
$y = ceil($x * 100) / 100; // 6.26


How can I incorporate that into my original statement of
$ad2day = round(($ad2day * $tax)+ $ad2day, 2);

$x = $ad2day * $tax;
$y = ceil($x * 100) / 100;
$ad2day = round($y + $ad2day, 2);
Replacing $y:

$x = $ad2day * $tax;
$ad2day = round((ceil($x * 100) / 100) + $ad2day, 2);
/* $y = ceil($x * 100) / 100; */

Replacing $x

$ad2day = round((ceil(($ad2day * $tax) * 100) / 100) + $ad2day, 2);
/* $x = $ad2day * $tax; */


Thanks Pedro, the replacing $x option is the one I was seeking. Works
like a charm. Much obliged for the help.
Jul 17 '05 #5

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

Similar topics

3
by: db_guy | last post by:
<? $a=1.5345493; $b=round($a,2); echo "b: ".$b; ?> This code should return "b: 1.54", but instead returns "b: 1.53". Can anyone else confirm this?
5
by: JustSomeGuy | last post by:
In MS Visual C++ 6.0 Where is M_PI defined? Where is round defined?
8
by: David Corby | last post by:
Hi everybody, I've got a problem. I'm trying to round a double to a particular number of significant digits, in this case 5, but I can't figure out a way around getting _exactly_ what I want...
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
6
by: Trevor Stapleton | last post by:
in a function i am working on i can end up with a variable named dwncount holding the value 2.5 or some other fraction, I know this can be rounded up, but how? or what is the syntax? TIA Trevor
14
by: Nils Grimsmo | last post by:
Why did round() change in Python 2.4? $ python2.3 Python 2.3.5 (#2, Jun 19 2005, 13:28:00) on linux2 >>> round(0.0225, 3) 0.023 >>> "%.3f" % round(0.0225, 3) '0.023' >>>
2
by: Marcos Jose Setim | last post by:
Hello, I need to round one number in language C, but I did not obtain. I already tried to use the: #include <math.h> ceil(value); But it's shown one error:
7
by: bravesplace | last post by:
Hello, I am using the folling funtion to round a number to a single digit on my form: function round1(num) { return Math.round(num*1)/1 }
0
by: Maric Michaud | last post by:
Le Monday 28 July 2008 02:35:08 Herman, vous avez écrit : Hmm, I don't have the same result in python2.6, I suspect it's a floating point precision problem, try just to type "0.5" in the console...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.