How to print with two decimals in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to print with two decimals in python?

123.45 Instead 123.456789

16th Sep 2021, 6:55 PM
Mahmoud ayman
Mahmoud ayman - avatar
11 Answers
+ 1
Mahmoud ayman yn = 123.456789 _2dec = "{:.2f}".format(yn) print(_2dec) Arsalan I don't believe he was speaking about shortening a string
16th Sep 2021, 7:12 PM
BroFar
BroFar - avatar
+ 1
a = str(1234.567) b = a[:a.find('.')+3] print(b) 1234.56
16th Sep 2021, 7:10 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 1
BroFar In his question, the number is not rounded.
16th Sep 2021, 7:19 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 1
If you asking about rounding number upto 2 decimal point, you can try any of this way.. print(round(123.456789,2)) #123.46 print(float("{:.2f}".format(123.456789))) #123.46 Or, If you asking about how to eliminate last four digits without rounding then you can do this, print(float(str(123.456789)[:6])) #123.45
16th Sep 2021, 7:27 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Mahmoud ayman Arsalan _2dec = "{:.3g}".format(yn)
16th Sep 2021, 7:27 PM
BroFar
BroFar - avatar
0
There are some ways to print a float points. I hope, it helps. Happy coding! https://code.sololearn.com/cRfySS5m9LXi/?ref=app
16th Sep 2021, 7:19 PM
mesarthim
mesarthim - avatar
0
BroFar yes, that is what I mean
16th Sep 2021, 10:06 PM
Mahmoud ayman
Mahmoud ayman - avatar
0
mesarthim yes, it helps very much
16th Sep 2021, 10:09 PM
Mahmoud ayman
Mahmoud ayman - avatar
0
Example using a simple function (for Python 3 only): https://code.sololearn.com/cUy0jc8xc7vc/?ref=app … instead of „print“ you can use „return“ as well and you can combine it with „float“ if needed.
17th Sep 2021, 2:17 PM
Stefan
Stefan - avatar
0
Try out this.. Just use round function "round (M , D)" Second parameter decided how many digits after decimals #code..... import math a=616.7655261 print(round(a,2))
17th Sep 2021, 3:39 PM
Kittu
Kittu - avatar
0
The output shall not be rounded @Kittu - just see the answer above mine.
17th Sep 2021, 10:54 PM
Stefan
Stefan - avatar