By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Round and Ceil problem

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.