Hack Forums
[Python] percentages - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] percentages (/showthread.php?tid=5847688)



percentages - -Matatan- - Jun 11, 2018

I been trying to mess with the percentage in python, but every time I get more confused. when I use 0.01 that's 1% so how can I calculate 1.5%? or even 0.05% (half a percent)

based on the formula to calculate percentage which I have found on Google I made these two functions. while I can calculate 15% etc, how will I go about calculating 1.5 0.5 and so?

PHP Code

<?php 
def calculate
(last_price, percent_buy):
 
   z = ('{0:.2f}'.format(float(last_price) * percent_buy))
 
   final = float(last_price) + float(z)
 
   return final

def calculate_perc(last_price, percent_buy):
 
   z = ('{0:.2f}'.format(float(last_price) * percent_buy))
 
   final = float(last_price)
 
   return final

def calculate_spend(main_balance, percent_buy):
 
   final = '${0:.2f}'.format(float(main_balance) * percent_buy)
 
   return final


$
174.80
$22.80 
if there is a link or some better way to get the percentage in % and the quantity of the number I want to calculate please point me there, my brain hurts.


RE: percentages - michalmonday - Jun 11, 2018

Hi, I'm not sure if that's what you're looking for but:

Code
def p(num,num2):
   return str(num / float(num2) * 100) + "%"

p(9, 10)

9/10 = 0.9 which is 90% and that's the return of this function (string is returned)


RE: percentages - -Matatan- - Jun 11, 2018

(Jun 11, 2018 07:07 PM)michalmonday Wrote:  Hi, I'm not sure if that's what you're looking for but:

Code
def p(num,num2):
   return str(num / float(num2) * 100) + "%"

p(9, 10)

9/10 = 0.9 which is 90% and that's the return of this function (string is returned)
what I need is more like the mathematical formula to properly get half and thirds percentages 1.3% 1.5% etc. or even half a percent 0.5% in python that's 50% I believe.


RE: percentages - vorticalbox - Jun 12, 2018

(Jun 11, 2018 06:47 PM)-Matatan- Wrote:  I been trying to mess with the percentage in python, but every time I get more confused. when I use 0.01 that's 1% so how can I calculate 1.5%? or even 0.05% (half a percent)

based on the formula to calculate percentage which I have found on Google I made these two functions. while I can calculate 15% etc, how will I go about calculating 1.5 0.5 and so?

PHP Code

<?php 
def calculate
(last_price, percent_buy):
 
   z = ('{0:.2f}'.format(float(last_price) * percent_buy))
 
   final = float(last_price) + float(z)
 
   return final

def calculate_perc(last_price, percent_buy):
 
   z = ('{0:.2f}'.format(float(last_price) * percent_buy))
 
   final = float(last_price)
 
   return final

def calculate_spend(main_balance, percent_buy):
 
   final = '${0:.2f}'.format(float(main_balance) * percent_buy)
 
   return final


$
174.80
$22.80 
if there is a link or some better way to get the percentage in % and the quantity of the number I want to calculate please point me there, my brain hurts.

a simple way to think of it is 1 == 100%, take the percent you want and divide by 100.

5% = 0.05
50 = 0.5
1.5% = 0.015
79% = 0.79


RE: percentages - michalmonday - Jun 13, 2018

(Jun 11, 2018 08:17 PM)-Matatan- Wrote:  what I need is more like the mathematical formula to properly get half and thirds percentages 1.3% 1.5% etc. or even half a percent 0.5% in python that's 50% I believe.

You could just test the function and you'll see that it does what you described.. That's the proper formula, divide 1 number by the other and multiply by 100. That's because percentage by definition stands for "ratio as a fraction of 100"