how to prevent float from rounding off? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to prevent float from rounding off?

I want to display the float 2.378024 as 2.37. Anyone one know how to achieve this?

11th Oct 2018, 2:59 PM
Kari Gakure
Kari Gakure - avatar
2 Answers
0
You have to give a hint that you want to know it precisely. If you write cout << 5/2;, you will get 2 as a result. If you write 5.0/2 or 5/2.0, then you get the decimal too. It's a different datatype, like Roel said, so if you want to store a decimal, you can't write int x = 5.3;, but have to write double x = 5.3; (plus a few other options).
11th Oct 2018, 4:17 PM
HonFu
HonFu - avatar
0
you could use double which stores decimal numbers more precise edit. I accidently deleted my first post
11th Oct 2018, 4:19 PM
Roel
Roel - avatar