0

I am trying to search though text to find Money EG £12.30 take the numbers and then sum them.I have managed to get to the point where I have a list of floats but I can't seem to get them to 2 decimal points. However like in the code below if I specify an element in the list like [0] for example then it formats that element to 2 decimal points.

So my question would be: How can I format the whole list to 2 decimal places bearing in mind I will have no idea how long the list will be.

import re

num_regex = re.compile(r'\d\d.\d\d')
file_name = input("Enter File Name to open: ")

text_file = open(file_name, 'r')
search_text = text_file.read()
search = num_regex.findall(search_text)
print("Numbers found:", search,)
new_search =[]
for item in search:
    new_search.append(float(item))
new_search[0] = "%.2f" % new_search[0]
print(new_search[0])
MattDMo
  • 100,794
  • 21
  • 241
  • 231
Dead_Ling0
  • 171
  • 1
  • 1
  • 13
  • 1
    Use the [`decimal`](https://docs.python.org/3/library/decimal.html) module instead of `float()`. – MattDMo Nov 02 '15 at 15:47
  • I have started reading it but it is much more complex I am very new to programming and it can be difficult to read such texts and actually take anything useful from them because of the jargon! – Dead_Ling0 Nov 02 '15 at 15:56
  • Money values are [fixed-point](https://en.wikipedia.org/wiki/Fixed-point_arithmetic), not floating-point. Using floating-point arithmetic on money is not wise because of the way computers represent floating-point values, which can lead to unexpected rounding. – PM 2Ring Nov 02 '15 at 15:57
  • SO confused what ever I will just make do with having it as 12.3 or what ever thanks a bunch :) – Dead_Ling0 Nov 02 '15 at 16:05
  • for item in resultList: (Decimal(item).quantize(Decimal('.02'))) NewResultList.append(item) is this right? – Dead_Ling0 Nov 02 '15 at 16:21
  • FWIW, here's a small demo of floating-point behaving unexpectedly: `a=[float(s) for s in '0.10 0.20 0.30'.split()];print(a[0]+a[1]-a[2] == 0.0)`. It prints `False`, even though `0.10+0.20-0.30==0.0` when you use exact arithmetic. Similarly, `print(0.1+0.2-0.3 == 0)` also prints `False`. Try `print(0.1+0.2-0.3)` to see why. – PM 2Ring Nov 03 '15 at 12:10

2 Answers2

2

How can I format the whole list to 2 decimal places bearing in mind I will have no idea how long the list will be.

You use a for loop, iterating over your list of numbers, perhaps inside a list comprehension. Consider each of these:

formatted_list = []
for item in new_search:
    formatted_list.append("%.2f"%item)
print("Formatted List:", formatted_list)

Or, equivalently:

formatted_list = ["%.2f"%item for item in new_search]
print("Formatted List:", formatted_list)

Here is your entire program, using list comprehensions:

import re

num_regex = re.compile(r'\d\d.\d\d')
file_name = input("Enter File Name to open: ")

text_file = open(file_name, 'r')
search_text = text_file.read()
search = num_regex.findall(search_text)
print("Numbers found:", search,)
new_search =[float(item) for item in search]
print("Numbers found:", new_search,)
formatted_search =["%.2f"%item for item in new_search]
print("Numbers found:", formatted_search,)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Just sorted it out here is the final thing https://gist.github.com/anonymous/1ed68f0071bdb4143941#file-expend-py – Dead_Ling0 Nov 02 '15 at 17:57
1

just format the variable before appending it to the list

item_price = []
price = float(input('What is the price of the item? '))
    fprice = '{:.2f}'.format(price)
    item_price.append(fprice)
Kaladin58
  • 11
  • 1