Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

percentages

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.
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)
(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.
(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

¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
(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"


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by realsunjesterApr 28, 2019 06:46 AM 0 128 Apr 28, 2019 06:46 AM
Last Post: realsunjester
[Shell] by cogamaJul 8, 2016 02:15 PM 4 123 Jul 8, 2016 02:15 PM
Last Post: cogama