0
How to print with two decimals in python?
123.45 Instead 123.456789
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
+ 1
a = str(1234.567)
b = a[:a.find('.')+3]
print(b)
1234.56
+ 1
BroFar
In his question, the number is not rounded.
+ 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
+ 1
Mahmoud ayman
Arsalan
_2dec = "{:.3g}".format(yn)
0
There are some ways to print a float points.
I hope, it helps.
Happy coding!
https://code.sololearn.com/cRfySS5m9LXi/?ref=app
0
BroFar yes, that is what I mean
0
mesarthim yes, it helps very much
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.
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))
0
The output shall not be rounded @Kittu - just see the answer above mine.