The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP Percentage
SilverMarc
post Mar 9 2009, 08:11 PM
Post #1





Group: Members
Posts: 1
Joined: 9-March 09
Member No.: 8,018



Simple for any brainy PHP person (which, you can see, isn't me...)

I have a number variable $small
And another number variable $big

I want to know what $percent the $small is to the $big, rounded off to the nearest 10th

Examples:

$small = 50
$big = 100
$percent = 50

or

$small = 74
$big = 100
$percent = 70

Easy to use php would be great - this must be simple for most of you!

Thanks.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 10 2009, 12:17 AM
Post #2


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



Basic trick for rounding: use floor(), which rounds down to a whole number.

So to round to multiples of 10, divide by 10, use floor(), then multiply by 10 again.

In this case you need to multiply by 100 to get a percentage, so use x10 then floor() then x10:

$percentageintens = 10*floor(10*$a/$b);

---
As the Donald Knuth quote says: "Beware of bugs in the above. I have only proved it correct -- I have not tested it."
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 16th April 2024 - 07:17 AM