12

I have a list of floats in Python and when I convert it into a string, I get the following

[1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001]

These floats have 2 digits after the decimal point when I created them (I believe so),

Then I used

str(mylist)

How do I get a string with 2 digits after the decimal point?

======================

Let me be more specific, I want the end result to be a string and I want to keep the separators:

"[1883.95, 1878.33, 1869.43, 1863.40]"

I need to do some string operations afterwards. For example +="!\t!".

Inspired by @senshin the following code works for example, but I think there is a better way

msg = "["

for x in mylist:
    msg += '{:.2f}'.format(x)+','

msg = msg[0:len(msg)-1]
msg+="]"
print msg
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Niebieski
  • 591
  • 1
  • 8
  • 16

6 Answers6

15

Use string formatting to get the desired number of decimal places.

>>> nums = [1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001]
>>> ['{:.2f}'.format(x) for x in nums]
['1883.95', '1878.33', '1869.43', '1863.40']

The format string {:.2f} means "print a fixed-point number (f) with two places after the decimal point (.2)". str.format will automatically round the number correctly (assuming you entered the numbers with two decimal places in the first place, in which case the floating-point error won't be enough to mess with the rounding).

senshin
  • 10,022
  • 7
  • 46
  • 59
  • That works, I got ['1883.95', '1878.33', '1869.43', '1863.40'] however, can I get something like "[1883.95, 1878.33, 1869.43, 1863.40]"? I need to some string operation afterwards. Thanks! – Niebieski May 02 '14 at 00:23
  • @Niebieski You want a string representation of the whole list? I very much doubt that that's what you _actually_ want, but you can just call `str()` on the list to get something close. – senshin May 02 '14 at 00:26
  • I want to do += "!\t!", or similar operation – Niebieski May 02 '14 at 00:31
  • @Niebieski But _why_ do you want to do that? Why wouldn't you just perform string operations on the individual numbers? – senshin May 02 '14 at 00:31
  • I am generating an email summary of this list, so I need the end result to be a string and I want to keep the separators. I am looking for an elegant way to do so. I thought str(mylist) would do it but it gives that annoying additional digits – Niebieski May 02 '14 at 00:40
3

If you want to keep full precision, the syntactically simplest/clearest way seems to be

mylist = list(map(str, mylist))
ezekiel
  • 427
  • 5
  • 20
2
map(lambda n: '%.2f'%n, [1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001])

map() invokes the callable passed in the first argument for each element in the list/iterable passed as the second argument.

1

Get rid of the ' marks:

>>> nums = [1883.95, 1878.3299999999999, 1869.4300000000001, 1863.4000000000001]
>>> '[{:s}]'.format(', '.join(['{:.2f}'.format(x) for x in nums]))
'[1883.95, 1878.33, 1869.43, 1863.40]'
  • ['{:.2f}'.format(x) for x in nums] makes a list of strings, as in the accepted answer.
  • ', '.join([list]) returns one string with ', ' inserted between the list elements.
  • '[{:s}]'.format(joined_string) adds the brackets.
Karl I.
  • 121
  • 5
0
str([round(i, 2) for i in mylist])
yan
  • 196
  • 1
  • 9
0

Using numpy you may do:

np.array2string(np.asarray(mylist), precision=2, separator=', ')