+ 4

How to print floating value of only last 2 round points in c++?

Here suppose a is float a = 500/33; If now I print a with c++ code cout << a; It will print 15.1515 But I just want to print only 15.15 Only the last 2 digits after the frictional period. Is there any alternative? NB: I would not like to use printf function. https://code.sololearn.com/cOqhJD7ZY5nR/?ref=app

27th Nov 2023, 5:37 PM
Md Safayet El Hossain
Md Safayet El Hossain - avatar
6 odpowiedzi
+ 5
You forgot to #include <iomanip>
27th Nov 2023, 6:19 PM
Tibor Santa
Tibor Santa - avatar
+ 2
1. Multiply by 100 2. Convert to int 3. Divide by 100.0
27th Nov 2023, 5:58 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Yes, there are many more options, this was the simplest. https://stackoverflow.com/questions/25925290/c-round-a-double-up-to-2-decimal-places
27th Nov 2023, 6:05 PM
Tibor Santa
Tibor Santa - avatar
+ 2
How to print floating value of only last 2 round points in c++?
28th Nov 2023, 7:44 PM
Angel Mae Cortino
Angel Mae Cortino - avatar
+ 1
I got a new idea. Thanks a lot. cout << fixed << setprecision(2); cout << gpa; from this code.
27th Nov 2023, 6:11 PM
Md Safayet El Hossain
Md Safayet El Hossain - avatar
27th Nov 2023, 6:24 PM
Md Safayet El Hossain
Md Safayet El Hossain - avatar