python int to float | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

python int to float

check the attached code. why it shows lengthy output while give an integer in place of float for example, input: 2,456 output: 182.85000000000002 expected: 182.85 https://code.sololearn.com/cXrAUDYfAtAQ/?ref=app

11th Dec 2021, 6:28 AM
Amal Benny
Amal Benny - avatar
6 Answers
+ 1
Amal Benny, This is because of how floating point numbers are represented in almost every programming languages. https://docs.python.org/3/tutorial/floatingpoint.html
11th Dec 2021, 7:52 AM
zexu knub
zexu knub - avatar
+ 1
Use: round(your_float, 2) so it will be float with 2 digits after a dot.
11th Dec 2021, 8:13 AM
Herr Rozwel
Herr Rozwel - avatar
0
Did you correct it? cuz i'm not seeing anything like that.
11th Dec 2021, 6:37 AM
zexu knub
zexu knub - avatar
0
No, there is no change after the posting of discussion. Please try the example given in a good python interpreter. for prefilled one check : https://ideone.com/cELp7M
11th Dec 2021, 6:40 AM
Amal Benny
Amal Benny - avatar
0
Amal Benny By default Python don't stop at double-precision after the 2 digits. sol- print("{:.2f}".format(dc_frk))
11th Dec 2021, 6:50 AM
zexu knub
zexu knub - avatar
0
Then what python mean by 00002 at the last part of result. How it happens.🤔 zexu knub
11th Dec 2021, 6:53 AM
Amal Benny
Amal Benny - avatar