# Float Division Returning Numerator or wrong number

I am reading the data from a financial statement in PDF format.

I have a couple numerical strings that i convert to floats simply using float(x). The strings do have decimal points and numbers afterward. However when i divide the to floats it is returning the numerator instead of actually dividing.

``````a = "5000.0"
b = "100.0"
cost = float(a)/float(b)
print("Cost: {:.2f)".format(cost))

Cost: 5000.00
``````
1. is there any idea why this is happening
2. what can i do to correct it.

Here is the block of code:

``````def getPrice(mults, cost, shares, stock, transType):
if mults == 1:
print(type(cost))
print(type(shares))
print("Cost: ", float(cost))
print("Shares: ", float(shares))
print("price: ", "{:.2f}".format(float(cost)/float(shares)))
return {stock[0]: {"Cost": cost, "Shares": shares, "Price": float(cost)/float(shares[0]), "Transaction Type" : transType}}

else:
prices = {}
for c, s, sym, t in zip(cost, shares, stock, transType):
prices[sym] = {"Cost": c, "Shares": s, "Price": float(c)/float(s), "Transaction Type": t}
return prices
``````

the call to the function is as follows:
price = getPrice(mults, cost, shares, stckSym, transType)

where mults is the number of transactions in a statement

print statements are:

``````<class 'str'>
<class 'str'>
Cost:  5000.0
Shares:  100.0
price:  5000.00
``````

What is weird is sometimes it will come up with the correct value. for example

``````cost = "398.98"
shares = "20.0"
float(cost)/float(shares) = 19.494
``````

so it works sometimes. and sometimes it does not.

Here is another result from a different statement:

``````Cost      Shares    Price
1256.20 | 220    | 628.1
``````

Let me know if i need to provide more information. This has been puzzling me all day.

Did you try:

``````print("price: ", "{:.2f}".format(float(float(cost)/float(shares))))
``````

or

``````print("price: ", "{:.2f}".format(float(cost/shares))
``````

it seems that the result of the operation might be being cast as a non-float, even though the individual vars are forced cast to float.

Here’s my test:

``````def getPrice(mults, cost, shares, stock, transType):
if mults == 1:
print(type(cost))
print(type(shares))
print("Cost: ", float(cost))
print("Shares: ", float(shares))
print("price: ", "{:.2f}".format(float(cost)/float(shares)))
return {stock[0]: {"Cost": cost, "Shares": shares, "Price": float(cost)/float(shares[0]), "Transaction Type" : transType}}

else:
prices = {}
for c, s, sym, t in zip(cost, shares, stock, transType):
prices[sym] = {"Cost": c, "Shares": s, "Price": float(c)/float(s), "Transaction Type": t}
return prices

print(getPrice(1,"5000","100","IBM",1))
print(getPrice(1,"398.98","20.0","AAPL",1))
print(getPrice(1,"1256.20","220","ZZZ",1))
``````

and the results under python 3.8:

``````<class 'str'>
<class 'str'>
Cost:  5000.0
Shares:  100.0
price:  50.00
{'I': {'Cost': '5000', 'Shares': '100', 'Price': 5000.0, 'Transaction Type': 1}}
<class 'str'>
<class 'str'>
Cost:  398.98
Shares:  20.0
price:  19.95
{'A': {'Cost': '398.98', 'Shares': '20.0', 'Price': 199.49, 'Transaction Type': 1}}
<class 'str'>
<class 'str'>
Cost:  1256.2
Shares:  220.0
price:  5.71
{'Z': {'Cost': '1256.20', 'Shares': '220', 'Price': 628.1, 'Transaction Type': 1}}
``````

This line:

``````        return {stock[0]: {"Cost": cost, "Shares": shares, "Price": float(cost)/float(shares[0]), "Transaction Type" : transType}}
``````

needs to be:

``````        return {stock[0]: {"Cost": cost, "Shares": shares, "Price": float(cost)/float(shares), "Transaction Type" : transType}}
``````

By dividing by shares[0], you’re dividing by the first number in the shares string.

If you’re using something other than python 3.8, you need to tell us.