+ 1

Round up to nearest cent

How do you round up to the nearest cent in C++?

25th Apr 2021, 10:41 PM
Benjamin Rios
Benjamin Rios - avatar
1 Antwort
+ 2
If you want to round a float to the nearest integer, you could use c++'s round function: https://www.cplusplus.com/reference/cmath/round/ ceil is a function that always rounds up. If you want to round to the nearest 2 digits, you could do something like: double x = 3.14159265358979; double rounded = round(x * 100) / 100.0; cout << rounded << endl; If you just need to print out the value, look at setprecision. An example is at: https://stackoverflow.com/questions/15327706/c-currency-output
25th Apr 2021, 10:56 PM
Josh Greig
Josh Greig - avatar