FutureQuest, Inc. FutureQuest, Inc. FutureQuest, Inc.

FutureQuest, Inc.
Go Back   FutureQuest Community > General Site Owner Support (All may read/respond) > PHP, Perl, Python and/or MySQL
User Name
Password  Lost PW

Reply
 
Thread Tools Search this Thread Display Modes
Old 06-16-1999, 09:06 PM   Postid: 42734
SneakyDave
Fond of TAZ
 
SneakyDave's Avatar

Forum Notability:
93 pts: Helpful Contributor
[Post Feedback]
 
Join Date: Feb 1999
Posts: 921
Rounding numbers in PHP

Sneaky's dumb question of the day.

I can use the round() function to round a number up to an integer, but what if I want to take a percentage of "sales", say 5% of $123.78? The answer is 6.189, but using printf(), I can only achieve 6.18, not the true amount of 6.19, rounded.

Is there a simple way I can round to the nearest cent?

Thanks
Sneaky
SneakyDave is offline   Reply With Quote
Old 06-16-1999, 09:30 PM   Postid: 42735
Justin
Visitor
 
Justin's Avatar

Forum Notability:
0 pts:
[Post Feedback]
 
Join Date: Jan 1999
Location: Kissimmee, FL
Posts: 3,672
Add half of your least significant digit and then do an intval() or sprintf():

7.557 rounded to the nearest cent would be 7.56 - so doing this would round to the nearest cent:

$num = 7.557;
$rounded = intval($num * 100 + .005) / 100;

Resulting in 7.56. Or to simply round to whole numbers, add .5:

$num = 8.77242985186345973654
$rounded = intval($num + .5);

Which would result in 9.

HTH

------------------
Justin Nelson
FutureQuest Support

[This message has been edited by Justin (edited 06-16-99)]
Justin is offline   Reply With Quote
Old 06-16-1999, 09:32 PM   Postid: 42736
SneakyDave
Fond of TAZ
 
SneakyDave's Avatar

Forum Notability:
93 pts: Helpful Contributor
[Post Feedback]
 
Join Date: Feb 1999
Posts: 921
Removed due to dumbness
[This message has been edited by SneakyDave (edited 06-16-99)]
SneakyDave is offline   Reply With Quote
Old 06-16-1999, 09:35 PM   Postid: 42737
SneakyDave
Fond of TAZ
 
SneakyDave's Avatar

Forum Notability:
93 pts: Helpful Contributor
[Post Feedback]
 
Join Date: Feb 1999
Posts: 921
RAHAHGHGHGHGH!

OK, I don't know what I'm doing wrong here...
Here's the code I'm using, I want to find 5% of the variable $out_fee, which is 17.77.

Code Sample:

//Get the percentage of the number, results in 0.8885
$out_fee = $out_fee * .05;
//Sprintf it down to 3 decimals, results in 0.889
$out_fee = sprintf("%01.3f",$out_fee);
//Try to round it up to 0.89
$rounded = intval($out_fee * 100 + .005) / 100;



The result I get in $rounded is .88, not .89, did I completely screw it up?
Thanks
Sneaky

[This message has been edited by SneakyDave (edited 06-16-99)]
SneakyDave is offline   Reply With Quote
Old 06-17-1999, 06:52 PM   Postid: 42738
stan
Site Owner
 
stan's Avatar

Forum Notability:
10 pts: User-friendly
[Post Feedback]
 
Join Date: Feb 1999
Location: Utrecht, NL
Posts: 101
Shouldn't that last line read


Code Sample:

$rounded = intval(($out_fee + .005) * 100) / 100;




- Stan
stan is offline   Reply With Quote
Old 06-17-1999, 07:15 PM   Postid: 42739
Justin
Visitor
 
Justin's Avatar

Forum Notability:
0 pts:
[Post Feedback]
 
Join Date: Jan 1999
Location: Kissimmee, FL
Posts: 3,672
Nope - order of operations   The muliplication is always done first. There is an error however   Since we are multiplying by 100, we need to add .5, not .005

So:

$rounded = intval($out_fee * 100 + .5) / 100;

is what you are looking for. BTW - no need to reduce it to 3 decimal places first - in fact, you shouldn't. First off, cutting 0.8885 to 3 places results in 0.888 - not 0.889

So you can omit that part of it - the result is an unnecessary string conversion that converts right back into a number on the next line - it never needs to become a string value in the process. PHP does convert strings to numbers and vice-versa on the fly so you usually don't see the extra overhead involved - the work is hidden. But it is there - which is why I use intval() instead of sprintf() to reduce the digits. Working with numbers is far less work for a computer than is working with strings.

HTH

------------------
Justin Nelson
FutureQuest Support
Justin is offline   Reply With Quote
Old 06-17-1999, 07:17 PM   Postid: 42740
Justin
Visitor
 
Justin's Avatar

Forum Notability:
0 pts:
[Post Feedback]
 
Join Date: Jan 1999
Location: Kissimmee, FL
Posts: 3,672
Stan - I see what you meant after looking again - you are right, that would also have fixed my little bug by adding .005 before the multiplication, which yeilds the same result as adding .5 after multiplying (.005 * 100 = .5). Sorry

Either way will work now

------------------
Justin Nelson
FutureQuest Support
Justin is offline   Reply With Quote
Old 06-17-1999, 11:01 PM   Postid: 42741
SneakyDave
Fond of TAZ
 
SneakyDave's Avatar

Forum Notability:
93 pts: Helpful Contributor
[Post Feedback]
 
Join Date: Feb 1999
Posts: 921
Hey guys, thanks for clearning that up, I've still got a little hair left, thanks to you.

Sneaky
SneakyDave is offline   Reply With Quote
Old 06-17-1999, 11:32 PM   Postid: 42742
SneakyDave
Fond of TAZ
 
SneakyDave's Avatar

Forum Notability:
93 pts: Helpful Contributor
[Post Feedback]
 
Join Date: Feb 1999
Posts: 921
But I still don't understand why this isn't working:
Look at this code:

Code Sample:

$out_fee = 1.275;
print "not rounded - $out_fee
";
$rounded = intval(($out_fee + .005) * 100) / 100;
print "rounded - $rounded
";



The output comes out:
not rounded - 1.275
rounded - 1.27

Shouldn't it be 1.28?

And on a related note, what if I don't know if the number is going to be 3 digits behind the decimal? To use intval, wouldn't I have to shorten it to the thousandths place to make the intval work right?

Thanks both for your time, again.
Sneaky
SneakyDave is offline   Reply With Quote
Old 06-17-1999, 11:49 PM   Postid: 42743
 Terra
CTO FutureQuest, Inc.
 
Terra's Avatar
 
Join Date: Jun 1998
Location: Z'ha'dum
Posts: 7,683
Pseudo Algorithm: (just thinking out loud, I'm NO mathematician)

Rounding arbitrary length decimal numbers

value: 1.2467
count :value: decimal places(DP) == 4
subtract 1 from DP == 3
raise 10 ^ 3 == 1000
multiply :value: by 1000 == 1246.7
apply PHP round function to :value: == 1247
divide :result: by 1000 == 1.247

--next example--
value: 1.275
DP == 3
10 ^ (DP - 1) == 100
1.275 == 127.5
round(127.5) == 128
128 / 100 == 1.28

--
Terra
--No muss, No fuss--
FutureQuest

PS: Feel free to rip this to shreds...
Terra is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 visitors)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 09:58 PM.


Running on vBulletin®
Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.
Hosted & Administrated by FutureQuest, Inc.
Images & content copyright © 1998-2013 FutureQuest, Inc.
FutureQuest, Inc.