I found that if I use round function to some float values, it is OK to round to two decimals, but sometimes, for example, round(myvalu, 2)
it will return 1.2
instead of 1.20
, but actually I want every returned value is rounded to two decimals, i.e. 1.20
instead of 1.2
. What should I do?
Asked
Active
Viewed 800 times
0

bastelflp
- 9,362
- 7
- 32
- 67

user5802211
- 197
- 1
- 9
-
Look at this post how to format floats: http://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python – Franco Oct 02 '16 at 23:19
-
Possible duplicate of [Rounding a number in python but keeping ending zeros](http://stackoverflow.com/questions/19986662/rounding-a-number-in-python-but-keeping-ending-zeros) – Mark Dickinson Oct 03 '16 at 19:13
1 Answers
1
That's something you'd do when displaying the number.
Try
'%.2f' % num

Nikhil Kothari
- 5,215
- 2
- 22
- 28
-
Thank you, but I am wondering whether there is a function that replaces the function round to get what I mentioned in the question? – user5802211 Oct 03 '16 at 08:09
-
1In terms of the actual number (the bits) 1.2 == 1.20... the only difference is the visual representation. – Nikhil Kothari Oct 03 '16 at 14:33
-
Also, look at round() ... 2nd are is # of decimal places. But you'll still need to format to get desired string representation. – Nikhil Kothari Oct 03 '16 at 14:36